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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React: Force act running always in sequence #30191

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions code/renderers/react/src/renderToCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ class ErrorBoundary extends ReactComponent<{

const Wrapper = FRAMEWORK_OPTIONS?.strictMode ? StrictMode : Fragment;

const actQueue: (() => Promise<void>)[] = [];
let isActing = false;

const processActQueue = async () => {
if (isActing || actQueue.length === 0) {
return;
}

isActing = true;
const actTask = actQueue.shift();
if (actTask) {
await actTask();
}
isActing = false;
processActQueue();
};

export async function renderToCanvas(
{
storyContext,
Expand Down Expand Up @@ -81,12 +98,22 @@ export async function renderToCanvas(
unmountElement(canvasElement);
}

await act(async () => {
await renderElement(element, canvasElement, storyContext?.parameters?.react?.rootOptions);
await new Promise<void>((resolve, reject) => {
try {
actQueue.push(async () => {
await act(async () => {
await renderElement(element, canvasElement, storyContext?.parameters?.react?.rootOptions);
resolve();
});
});
processActQueue();
valentinpalkovic marked this conversation as resolved.
Show resolved Hide resolved
} catch (e) {
reject(e);
}
});

return async () => {
await act(() => {
return () => {
act(() => {
valentinpalkovic marked this conversation as resolved.
Show resolved Hide resolved
unmountElement(canvasElement);
});
};
Expand Down
Loading