您应该绑定该
handleChange函数。您收到此错误的原因是,在handleChange函数中,
this键盘 *** 作未引用React类的上下文,因此您需要绑定该函数。
看到这个答案 why do you need to bind event handlers inReact
class HTMLEditor extends React.Component { constructor(props) { super(props); this.state = {value: 'Put here HTML'}; } handleChange = (e) =>{ this.setState({value: e.currentTarget.value}); } render() { return ( <div> <textarea defaultValue={this.state.value} onChange={ this.handleChange } /> <div>{this.state.value}</div> </div> ); }}ReactDOM.render( <HTMLEditor />, document.getElementById('container'));<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script><div id="container"></div>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)