Skip to content

Commit

Permalink
Merge pull request #19125 from storybookjs/react-18-followup
Browse files Browse the repository at this point in the history
React: Fix issue with react 18 implementation
  • Loading branch information
shilman committed Sep 14, 2022
1 parent cba592e commit 6b2bd0a
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions app/react/src/client/preview/render.tsx
Expand Up @@ -39,10 +39,10 @@ const WithCallback: FC<{ callback: () => void; children: ReactElement }> = ({
children,
}) => {
// See https://github.com/reactwg/react-18/discussions/5#discussioncomment-2276079
const once = useRef(false);
const once = useRef<() => void>();
useLayoutEffect(() => {
if (once.current) return;
once.current = true;
if (once.current === callback) return;
once.current = callback;
callback();
}, [callback]);

Expand All @@ -55,11 +55,7 @@ const renderElement = async (node: ReactElement, el: Element) => {

return new Promise((resolve) => {
if (root) {
root.render(
<WithCallback key={Math.random()} callback={() => resolve(null)}>
{node}
</WithCallback>
);
root.render(<WithCallback callback={() => resolve(null)}>{node}</WithCallback>);
} else {
ReactDOM.render(node, el, () => resolve(null));
}
Expand Down

0 comments on commit 6b2bd0a

Please sign in to comment.