价值不是写入此的。
在React页面上,我有一种方法正在尝试设置状态。但是,它似乎不起作用(请参阅console.log
)。我在做什么错?
editPlan = (cell, row) => {
return (
<img
src={edit}
alt="edit"
onClick={() => {
console.log(row.id); // prints 2
this.setState({ editId: row.id, openEditModal: true });
console.log(JSON.stringify(this.state.editId)); // prints "". I would expect 2.
console.log(JSON.stringify(this.state.openEditModal)); // prints false. I would expect true.
this.props.getPlan(row.id);
}}
/>
);
};
On a React page I have a method by means of which I'm trying to set the state. However, it doesn't seem to work (see the console.log
). What am I doing wrong?
editPlan = (cell, row) => {
return (
<img
src={edit}
alt="edit"
onClick={() => {
console.log(row.id); // prints 2
this.setState({ editId: row.id, openEditModal: true });
console.log(JSON.stringify(this.state.editId)); // prints "". I would expect 2.
console.log(JSON.stringify(this.state.openEditModal)); // prints false. I would expect true.
this.props.getPlan(row.id);
}}
/>
);
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
setState
不同步 - 它只是排队您所需的状态更改,并以后将其进行优化,以优化组件将如何重新渲染。您可以将函数回调为最后一个参数,当状态更改发生时将被调用。
请参阅: https://reaectjs.s.s.org/docs/react-compont.html.html#setstate < /a>
setState
works asynchronously -- it just queues up your desired state change and will get to it later to optimize how your component will re-render.You can pass a callback to the function as the last argument which will be called when the state change has occurred.
See: https://reactjs.org/docs/react-component.html#setstate
您可以尝试使用状态回调函数,因为 setState工作异步
You can try using the state callback function because setState works asynchronously