价值不是写入此的。

发布于 2025-02-13 03:18:26 字数 704 浏览 2 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜唯美灬不弃 2025-02-20 03:18:26

setState不同步 - 它只是排队您所需的状态更改,并以后将其进行优化,以优化组件将如何重新渲染。

您可以将函数回调为最后一个参数,当状态更改发生时将被调用。

this.setState(
  { editId: row.id, openEditModal: true },
  () => {
    console.log(JSON.stringify(this.state.editId));
    console.log(JSON.stringify(this.state.openEditModal));
  }
);

请参阅: 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.

this.setState(
  { editId: row.id, openEditModal: true },
  () => {
    console.log(JSON.stringify(this.state.editId));
    console.log(JSON.stringify(this.state.openEditModal));
  }
);

See: https://reactjs.org/docs/react-component.html#setstate

寂寞花火° 2025-02-20 03:18:26

您可以尝试使用状态回调函数,因为 setState工作异步

this.setState({ editId: row.id, openEditModal: true },()=>{
      console.log(JSON.stringify(this.state.editId));          
      console.log(JSON.stringify(this.state.openEditModal));  
      this.props.getPlan(row.id);
  });

You can try using the state callback function because setState works asynchronously

this.setState({ editId: row.id, openEditModal: true },()=>{
      console.log(JSON.stringify(this.state.editId));          
      console.log(JSON.stringify(this.state.openEditModal));  
      this.props.getPlan(row.id);
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文