无法使用React JS将名称添加到列表中

时间:2018-01-10 11:09:38

标签: javascript html reactjs

我正在尝试遵循ReactJS教程,因为这是我第一次进入框架,尝试将其改编为我见过的另一种风格。

但是我似乎无法让以下工作。这是一个简单的type a name > click button > add to displayed list应用程序,但在将样式更改为一个我感觉更舒服之后,我似乎无法获得添加。

Please see my Fiddle for the code.

This is the tutorial code that I am following.

1 个答案:

答案 0 :(得分:0)

App组件中的addFriend函数不会引用参数friend。此外,当您使用prevState值更新状态时,请遵循setState的更新程序函数语法。将其更改为

 addFriend = (friend) => {
    this.setState(prevState => ({
      friends: prevState.friends.concat([friend])
    }))
  }

<强> Working codesandbox