Skip to content

Commit

Permalink
fix: react 18 render async renderImperatively ref allways null (#1069)
Browse files Browse the repository at this point in the history
Co-authored-by: Yang <yangpan@zhongan.com>
  • Loading branch information
Yang03 and Yang committed Mar 20, 2023
1 parent d7a6f8d commit 9d9461a
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/zarm/src/utils/dom/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ const fullClone = {
const { version, render: reactRender, unmountComponentAtNode } = fullClone;

let createRoot: CreateRoot;
let flushSync;
try {
const mainVersion = Number((version || '').split('.')[0]);
if (mainVersion >= 18 && fullClone.createRoot) {
createRoot = fullClone.createRoot;
flushSync = fullClone.flushSync;
}
} catch {
// Do nothing;
Expand Down Expand Up @@ -53,7 +55,9 @@ function modernRender(node: React.ReactElement, container: ContainerType) {
toggleWarning(true);
const root = container[MARK] || createRoot(container);
toggleWarning(false);
root.render(node);
flushSync(() => {
root.render(node);
})
container[MARK] = root;
}

Expand Down

0 comments on commit 9d9461a

Please sign in to comment.