26
Here’s everything you need to know about the upcoming React v.18
All of these updates listed are primarily aimed at maintainers of third-party libraries.
You do not need to worry about these or start implementing in your production apps just yet because it’s still in the Alpha phase and library authors are testing it.
The ‘batching’ process starts when React starts grouping multiple updates in your state into a single re-render for better performance. This is great for performance because it avoids unnecessary re-rendering of components.
createRoot
, by which all updates will be automatically batched, no matter where they originate from.function handleClick() {
setCount(c => c + 1);
setFlag(f => !f);
}
setTimeout()
:setTimeout(() => {
setCount(c => c + 1);
setFlag(f => !f);
}, 1000);
fetch()
:fetch(/*...*/).then(() => {
setCount(c => c + 1);
setFlag(f => !f);
})
elm.addEventListener('click', () => {
setCount(c => c + 1);
setFlag(f => !f);
});
In a nutshell, a Transition is an update of a state of an app. These updates can be of two types: Urgent and Transition.
setSearchQuery(input);
// Urgent
setInputValue(input);
// Not urgent
setSearchQuery(input);
startTransition()
API, this issue can be solved by marking each of the updates as “transitions”.import { startTransition } from 'react';
// Urgent update
setInputValue(input);
startTransition(() => {
// Our initial state
setSearchQuery(input);
});
pipeToNodeWritable
while currently we have <Suspense>
.Server-side Rendering or SSR lets you generate HTML from React components on the server. One big benefit here is that the users see the page’s content before your JavaScript bundle loads and runs.
<Suspense>
. Here’s an example:<Layout>
<NavBar />
<Sidebar />
<RightPane>
<Post />
<Suspense fallback={<Spinner />}>
<Comments />
</Suspense>
</RightPane>
</Layout>
<Comments />
component into <Suspense />
, we tell React that it doesn’t need to wait for comments to start rendering the HTML for the rest of the webpage. So you can already interact with the all other components while the <Comments />
will show a spinner till it loads.<Suspense>
lets you hydrate the app even before the comment widget has loaded. In selective hydration, the non-interactive component doesn’t show up initially, then React hydrates it after the code has finished loading.<Comments />
component.<Suspense />
happens with tiny gaps in which the browser can handle events. By this, the event is handled immediately, and the browser doesn’t appear ‘stuck’ during a long hydration. This is a good performance improvement for low-end devices.<Suspense />
close to the root of your app:<Layout>
<NavBar />
<Suspense fallback={<BigSpinner />}>
<Suspense fallback={<SidebarGlimmer />}>
<Sidebar />
</Suspense>
<RightPane>
<Post />
<Suspense fallback={<CommentsGlimmer />}>
<Comments />
</Suspense>
</RightPane>
</Suspense>
</Layout>
<NavBar>
content. Everything else you see will hydrate as soon as the code block related to it is loaded.