我有一个React18项目,想要制作自定义react-formio组件(代码如下),但detachReact方法中的this.container.unmount显示此警告:
警告:在 React 已经渲染时尝试同步卸载根。在当前渲染完成之前,React 无法完成卸载根,这可能会导致竞争条件。
有什么方法可以在不发出警告的情况下卸载元素吗?
谢谢。
代码:
export default class Grid extends ReactComponent {
...
...
attachReact(element) {
this.container = createRoot(element)
return this.container.render(
<HistoryRouter history={history}>
<GridComponent {...{ ...this }} />
</HistoryRouter>
)
}
detachReact(element) {
if (element) {
this.container.unmount() // this creates the race condition warning
}
}
}
您可以使用 setTimemout 或 debounce 使其异步,这对我有用