[英]How to access the updated state after setState method with react hooks?
[英]Why my state is not getting updated even after writing the setState method?
从“反应”导入反应,{组件}
class 列扩展组件{构造函数(道具){超级(道具)
this.state={
message:'Hello'
}
}
changeMessage(){
this.setState=({
message:'Welcome'
})
}
render(){
return(
<div>
<div>{this.state.message}</div>
<button onClick={this.changeMessage}>Click</button>
</div>
)
}
} 导出默认列
正如上面的 ray hatfield 所说,你失去了this
上下文,需要使用箭头 function,但你也没有调用setState
; 你正在覆盖它。
删除=
从
this.setState=({
message:'Welcome'
})
所以它说:
this.setState({
message:'Welcome'
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.