【发布时间】:2020-01-26 17:52:54
【问题描述】:
如何将“this”与 setState 回调绑定。 react 允许吗?
this.state = {
selectedId: null,
}
//On click of some list item as
<ListGroup.Item
action href=""
key={id}
onClick={()=>
this.setState({selectedId: id.ID}), () => console.log(this.state.selectedID)
}
>
LIST ITEM
</ListGroup.Item>)
【问题讨论】:
-
我看不出你的代码有什么问题,你能详细说明你的问题是什么吗?
-
什么不起作用?
-
你必须提供一个更完整的例子并解释什么是行不通的。
-
@ShmiliBreuer 我想将此与 seState 回调绑定。目前,console.log 给我这个错误“Uncaught TypeError: Cannot read property 'state' of undefined”,这很可能是指谈论绑定 this
-
请看我的回答。
标签: reactjs