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 8, 2022
2 parents 64dd6b1 + 4283383 commit bae212c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
14 changes: 7 additions & 7 deletions code/lib/store/template/stories/rendering.stories.ts
Expand Up @@ -30,13 +30,13 @@ export const ChangeArgs = {
await button.focus();
await expect(button).toHaveFocus();

// // When we change the args to the button, it should not rerender
// await channel.emit('updateStoryArgs', { storyId: id, updatedArgs: { children: 'New Text' } });
// await within(canvasElement).findByText(/New Text/);
// await expect(button).toHaveFocus();
// When we change the args to the button, it should not rerender
await channel.emit('updateStoryArgs', { storyId: id, updatedArgs: { children: 'New Text' } });
await within(canvasElement).findByText(/New Text/);
await expect(button).toHaveFocus();

// await channel.emit('resetStoryArgs', { storyId: id });
// await within(canvasElement).findByText(/Click me/);
// await expect(button).toHaveFocus();
await channel.emit('resetStoryArgs', { storyId: id });
await within(canvasElement).findByText(/Click me/);
await expect(button).toHaveFocus();
},
};
12 changes: 4 additions & 8 deletions code/renderers/react/src/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 bae212c

Please sign in to comment.