-
Notifications
You must be signed in to change notification settings - Fork 47.4k
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 18: How to "wait" for concurrent mode #22836
Comments
I think what you're looking for is This API already existed since React 16.9 where you needed it for waiting for In React 18 you need So what you're looking for in your code samples is probably something like function App({ callback }) {
// Callback will be called when the div is first created.
return (
<div ref={callback}>
<h1>Hello World</h1>
</div>
);
}
const rootElement = document.getElementById("root");
const root = ReactDOM.createRoot(rootElement);
act(() => {
root.render(<App />);
});
console.log('rendered'); Does this address your questions? Further reading: |
Completely. But look like the current "docs" around React 18 API might require a little update. |
Well, actually there is another side of this problem, which might need a little different solution. This issue was been created during my investigations around With some things fixed, we were able to notice that due to #22692 (comment) React is "recreating" the content of the nearest Suspense boundary. Ie destroyng the old content and creating the new. Something not visible to the eye, but causing extra CPU(Paint) work and increasing LCP. export const RouteHydrationReport: RC = () => {
if (process.env.TARGET_IS_CLIENT) {
// eslint-disable-next-line react/no-danger
return <span dangerouslySetInnerHTML={{ __html: "" }} suppressHydrationWarning />;
}
return (
<span>
<span
data-testid="route-hydration-report"
data-route-rendered-at={process.env.TARGET_IS_CLIENT ? "client" : "server"}
/>
</span>
);
}; The presence of a given marker in HTML code is an indication of correct hydration. Absence is an indication of client-side transition or incorrect hydration. We found that sometimes(1/10 runs or fewer) React discards nested Suspense boundaries(the ones with HTML), usually keeping only the top-one. We also found that using React(dev-tools) Profiler almost always causes the problem. That lead to understanding that any other work(third party scripts) on the page should be paused until React finishes rendering, as any modifications from third party scripts(we don't know which and why) can interrupt the hydration process. If you read it from just a little differently, then the meaning of the paragraph above is the following - one should almost wait for initial hydration to complete before doing any other work, which requires such wait to have "first-class" support. Discovered frictions:
|
The question is driven by:
Render Callbacks
at Replacing render with createRoot reactwg/react-18#5Given advice is working as expected, and waiting for callback results the expected HTML to be presented in DOM, except:
useEffect
Wondering, is there any way to:
It's also worth clarifying that in the given example React is working in a little synthetic environment, which is neither real browser nor a unit test. The existence of such a test should be questioned in the first place.
The text was updated successfully, but these errors were encountered: