Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

React: Fix issue with react 18 implementation #19125

Merged
merged 4 commits into from Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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