Javascript React.JS无法读取属性';道具';零误差 刚开始使用react.js
我总是犯同样的错误。不知道为什么Javascript React.JS无法读取属性';道具';零误差 刚开始使用react.js,javascript,reactjs,Javascript,Reactjs,我总是犯同样的错误。不知道为什么 错误为:uncaughttypeerror:无法读取null的属性“props” 我知道当我点击按钮时会发生错误。具体地说,我认为它在update()方法中 JS代码: 从“React”导入React; 从“react dom”导入react dom; 类应用程序扩展了React.Component{ 构造函数(){ 超级(); this.state={递增:false} this.do_update=this.update.bind(this); } 更新()
错误为:
uncaughttypeerror:无法读取null的属性“props”
我知道当我点击按钮时会发生错误。具体地说,我认为它在update()方法中 JS代码:
从“React”导入React;
从“react dom”导入react dom;
类应用程序扩展了React.Component{
构造函数(){
超级();
this.state={递增:false}
this.do_update=this.update.bind(this);
}
更新(){
ReactDOM.render(
,
document.getElementById('app')
);
}
组件将接收道具(下一步){
this.setState({递增:(nextrops.val>this.props.val)})
}
render(){
返回{this.props.val}
}
}
App.defaultProps={val:0}
ReactDOM.render(,document.getElementById('app'))
在这种情况下,您需要传递到onClick
do\u update
,而不是update
return <button onClick={ this.do_update }>{ this.props.val }</button>
调用更新处理程序时,将无法获取“this”关键字的正确上下文。。。这就是您的代码可能具有以下特性的原因:
this.do_update = this.update.bind(this);
线路
如果将渲染方法更改为以下方式,则该方法应能正常工作:
render(){
return <button onClick={this.do_update}>{this.props.val}</button>
}
render(){
返回{this.props.val}
}
将此行this.state={increating:false}
更改为this.state={increating:false}
冒号后面的空格很重要 将渲染方法中的{this.update}更改为{this.do_update}为什么从不使用
this.do_update
?要更新状态时,请使用this.setState({reasing:false})
this.do_update = this.update.bind(this);
render(){
return <button onClick={this.do_update}>{this.props.val}</button>
}