React 的状态如何工作?
How does the state of React work?
我的代码在这里: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 ?
}));
为什么点击按钮后输入的文字没有被清除?
原因是,您正在使用 uncontrolled element (because you didn't define the value property with input element), to make it as controlled element 您需要使用输入元素指定值道具。
像这样:
<input type="text" value={this.props.text} onChange={this.handleTextChange}/>
检查工作fiddle。
我的代码在这里: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 ?
}));
为什么点击按钮后输入的文字没有被清除?
原因是,您正在使用 uncontrolled element (because you didn't define the value property with input element), to make it as controlled element 您需要使用输入元素指定值道具。
像这样:
<input type="text" value={this.props.text} onChange={this.handleTextChange}/>
检查工作fiddle。