如果React的类组件无法及时获得更新的状态,该怎么办?

发布于 2025-02-10 22:31:24 字数 499 浏览 0 评论 0原文

我有这样的代码。

Modal.confirm({
    tilte: "title",
    content: (
        <>
            <input onChange={
                () => this.setState({ A: true });
            }></input>
            
            this.state.A && <div>text</div>
        </>
    )
})

目的是在输入更改后,将显示&lt; div&gt; div&lt;/div&gt;,但发现状态a在之后无法及时获得更改,导致div未显示的内容。如何修改它? ?

I have a piece of code like this.

Modal.confirm({
    tilte: "title",
    content: (
        <>
            <input onChange={
                () => this.setState({ A: true });
            }></input>
            
            this.state.A && <div>text</div>
        </>
    )
})

The purpose is that after the input changes, <div>text</div> will be displayed, but it is found that state A cannot be obtained in time after the change, resulting in the content in div not being displayed. How can I modify it? ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

嗳卜坏 2025-02-17 22:31:24

问题:
您正在使用命令式(modal.confirm)显示modal,而state更改不触发modal重新渲染。
解决方案:

模态可以声明显示,例如:

class Test extends React.Component {
  state = {
 A: false,
 visible: false
  };
  handleClick = () => {
 this.setState({ visible: true });
  };
  render() {
 const { visible } = this.state;
 return (
   <>
     <Button onClick={this.handleClick}>Show Modal</Button>
     <Modal
       visible={visible}
       onCancel={() => this.setState({ visible: false })}
     >
       <input onChange={() => this.setState({ A: true })}></input>
       {this.state.A && <div>text</div>}
     </Modal>
   </>
 );
  }
}

modal.confirm显示的内容提取到组件

class ConfirmContent extends React.Component {
  state = {
 A: false
  };
  render() {
 return (
   <>
     <input onChange={() => this.setState({ A: true })}></input>
     {this.state.A && <div>text</div>}
   </>
 );
  }
}

//
Modal.confirm({
  tilte: "title",
  content: <ConfirmContent />
});
//

根本原因:react> react react 是一棵光纤树,这种命令率通常会重新创建光纤树(内部将调用croterootreactdom.render),您的问题是更新操作为用Fiber tree1触发的光纤触发(Fiber您在app.tsx中创建的树),而不是fiber on Fiber2 update(您使用确认)的光纤树,一个组件对应于fiber,它们在它们之后属于该光纤树通过creatorootreactdom.render s

Question:
You are using imperative (Modal.confirm) to display the Modal, and the state change does not trigger the Modal to re-render.
solution:

Modal can be displayed declaratively, for example:

class Test extends React.Component {
  state = {
 A: false,
 visible: false
  };
  handleClick = () => {
 this.setState({ visible: true });
  };
  render() {
 const { visible } = this.state;
 return (
   <>
     <Button onClick={this.handleClick}>Show Modal</Button>
     <Modal
       visible={visible}
       onCancel={() => this.setState({ visible: false })}
     >
       <input onChange={() => this.setState({ A: true })}></input>
       {this.state.A && <div>text</div>}
     </Modal>
   </>
 );
  }
}

Extract the content displayed by Modal.confirm into a component

class ConfirmContent extends React.Component {
  state = {
 A: false
  };
  render() {
 return (
   <>
     <input onChange={() => this.setState({ A: true })}></input>
     {this.state.A && <div>text</div>}
   </>
 );
  }
}

//
Modal.confirm({
  tilte: "title",
  content: <ConfirmContent />
});
//

Root cause: The update granularity of React is a fiber tree, such an imperative method often recreates a fiber tree (internally it will call createRoot, or ReactDom.render), your question That's when the update operation is triggered with a fiber on fiber tree1 (the fiber tree you created yourself in App.tsx), not with fiber on fiber2 Update (the fiber tree you created with confirm), a component corresponds to a fiber, they belong to that fiber tree after they are passed a createRoot or ReactDom.renders

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文