React的状态如何运作?

时间:2017-05-12 08:31:19

标签: reactjs state

我的代码在这里:https://jsfiddle.net/woyuditan26/bgwrfLxh/

      this.setState((prevState) => ({
        tasks: prevState.tasks.concat(newTask),
        text: ''  ////  why the input text is not cleared when I clicked the button ?
      }));

单击按钮时为什么不清除输入文本?

1 个答案:

答案 0 :(得分:2)

原因是,您正在使用uncontrolled element(因为您没有使用input元素定义value属性),要使其为controlled element,您需要使用input元素指定值props

像这样:

<input type="text" value={this.props.text} onChange={this.handleTextChange}/>

检查工作fiddle