如果React的类组件无法及时获得更新的状态,该怎么办?
我有这样的代码。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题:
您正在使用命令式
(modal.confirm)
显示modal
,而state
更改不触发modal
重新渲染。解决方案:
模态
可以声明显示,例如:将
modal.confirm
显示的内容提取到组件根本原因:
react> react react 是一棵光纤树,这种命令率通常会重新创建光纤树(内部将调用
或croteroot
reactdom.render
),您的问题是更新操作为用Fiber
tree1触发的光纤触发(Fiber
您在app.tsx
中创建的树),而不是fiber
onFiber2
update(您使用确认
)的光纤树,一个组件对应于fiber
,它们在它们之后属于该光纤树通过creatoroot
或reactdom.render
sQuestion:
You are using imperative
(Modal.confirm)
to display theModal
, and thestate
change does not trigger theModal
to re-render.solution:
Modal
can be displayed declaratively, for example:Extract the content displayed by
Modal.confirm
into a componentRoot cause: The update granularity of
React
is a fiber tree, such an imperative method often recreates a fiber tree (internally it will callcreateRoot
, orReactDom.render
), your question That's when the update operation is triggered with a fiber onfiber
tree1 (thefiber
tree you created yourself inApp.tsx
), not withfiber
onfiber2
Update (the fiber tree you created withconfirm
), a component corresponds to afiber
, they belong to that fiber tree after they are passed acreateRoot
orReactDom.render
s