获取错误无法读取未定义的属性“ setState”

获取错误无法读取未定义的属性“ setState”,第1张

获取错误无法读取未定义的属性“ setState”

您应该绑定

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>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5646506.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存