React v19 – React

Warning: An error occurred during hydration. The server HTML was replaced with client content in

.

Warning: Text content did not match. Server: “Server” Client: “Client”
at span
at App

Warning: An error occurred during hydration. The server HTML was replaced with client content in

.

Uncaught Error: Text content does not match server-rendered HTML.
at checkForUnmatchedText

We now log a single message with a diff of the mismatch:

Uncaught Error: Hydration failed because the server rendered HTML didn’t match the client. As a result this tree will be regenerated on the client. This can happen if an SSR-ed Client Component used:

– A server/client branch if (typeof window !== 'undefined').
– Variable input such as Date.now() or Math.random() which changes each time it’s called.
– Date formatting in a user’s locale which doesn’t match the server.
– External changing data without sending a snapshot of it along with the HTML.
– Invalid HTML tag nesting.

It can also happen if the client has a browser extension installed which messes with the HTML before React loaded.





+ Client
Server

at throwOnHydrationMismatch

as a provider

In React 19, you can render as a provider instead of :

const ThemeContext = createContext('');

function App({children}) {

return (

<ThemeContext value="dark">

{children}

ThemeContext>

);

}

New Context providers can use and we will be publishing a codemod to convert existing providers. In future versions we will deprecate .

Cleanup functions for refs

We now support returning a cleanup function from ref callbacks:

<input

ref={(ref) => {

return () => {

};

}}

/>

When the component unmounts, React will call the cleanup function returned from the ref callback. This works for DOM refs, refs to class components, and useImperativeHandle.

Note

Previously, React would call ref functions with null when unmounting the component. If your ref returns a cleanup function, React will now skip this step.

In future versions, we will deprecate calling refs with null when unmounting components.

Due to the introduction of ref cleanup functions, returning anything else from a ref callback will now be rejected by TypeScript. The fix is usually to stop using implicit returns, for example:

- <div ref={current => (instance = current)} />

+ <div ref={current => {instance = current}} />

The original code returned the instance of the HTMLDivElement and TypeScript wouldn’t know if this was supposed to be a cleanup function or if you didn’t want to return a cleanup function.

You can codemod this pattern with no-implicit-ref-callback-return.

useDeferredValue initial value

We’ve added an initialValue option to useDeferredValue:

function Search({deferredValue}) {

const value = useDeferredValue(deferredValue, '');

return (

<Results query={value} />

);

}

When initialValue is provided, useDeferredValue will return it as value for the initial render of the component, and schedules a re-render in the background with the deferredValue returned.

For more, see useDeferredValue.

Support for Document Metadata

In HTML, document metadata tags like </code>, <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><link/></code>, and <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><meta/></code> are reserved for placement in the <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"/> section of the document. In React, the component that decides what metadata is appropriate for the app may be very far from the place where you render the <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"/> or React does not render the <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"/> at all. In the past, these elements would need to be inserted manually in an effect, or by libraries like <a href="https://github.com/nfl/react-helmet" target="_blank" rel="nofollow noopener noreferrer" class="inline text-link dark:text-link-dark border-b border-link border-opacity-0 hover:border-opacity-100 duration-100 ease-in transition leading-normal"><code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">react-helmet</code></a>, and required careful handling when server rendering a React application.</p> <p class="whitespace-pre-wrap my-4">In React 19, we’re adding support for rendering document metadata tags in components natively:</p> <p><!--$--></p> <div dir="ltr" class="sandpack sandpack--codeblock rounded-2xl h-full w-full overflow-x-auto flex items-center bg-wash dark:bg-gray-95 shadow-lg my-8" style="contain:content"> <div class="sp-wrapper"> <div class="sp-stack"> <div class="sp-code-editor"> <pre class="sp-cm sp-pristine sp-javascript flex align-start"><code class="sp-pre-placeholder grow-[2]"><p><span class="sp-syntax-keyword">function</span> <span class="sp-syntax-definition">BlogPost</span><span class="sp-syntax-punctuation">(</span><span class="sp-syntax-punctuation">{</span><span class="sp-syntax-property">post</span><span class="sp-syntax-punctuation">}</span><span class="sp-syntax-punctuation">)</span> <span class="sp-syntax-punctuation">{</span><br/></p><p> <span class="sp-syntax-keyword">return</span> <span class="sp-syntax-punctuation">(</span><br/></p><p> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-tag">article</span><span class="sp-syntax-punctuation">></span><br/></p><p> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-tag">h1</span><span class="sp-syntax-punctuation">></span><span class="sp-syntax-punctuation">{</span><span class="sp-syntax-plain">post</span>.<span class="sp-syntax-property">title</span><span class="sp-syntax-punctuation">}</span><span class="sp-syntax-punctuation"/><span class="sp-syntax-tag">h1</span><span class="sp-syntax-punctuation">></span><br/></p><p> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-tag">title</span><span class="sp-syntax-punctuation">></span><span class="sp-syntax-punctuation">{</span><span class="sp-syntax-plain">post</span>.<span class="sp-syntax-property">title</span><span class="sp-syntax-punctuation">}</span><span class="sp-syntax-punctuation"/><span class="sp-syntax-tag">title</span><span class="sp-syntax-punctuation">></span><br/></p><p> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-tag">meta</span> <span class="sp-syntax-property">name</span>=<span class="sp-syntax-string">"author"</span> <span class="sp-syntax-property">content</span>=<span class="sp-syntax-string">"Josh"</span> <span class="sp-syntax-punctuation">/></span><br/></p><p> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-tag">link</span> <span class="sp-syntax-property">rel</span>=<span class="sp-syntax-string">"author"</span> <span class="sp-syntax-property">href</span>=<span class="sp-syntax-string">"</span> <span class="sp-syntax-punctuation">/></span><br/></p><p> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-tag">meta</span> <span class="sp-syntax-property">name</span>=<span class="sp-syntax-string">"keywords"</span> <span class="sp-syntax-property">content</span>=<span class="sp-syntax-punctuation">{</span><span class="sp-syntax-plain">post</span>.<span class="sp-syntax-property">keywords</span><span class="sp-syntax-punctuation">}</span> <span class="sp-syntax-punctuation">/></span><br/></p><p> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-tag">p</span><span class="sp-syntax-punctuation">></span><br/></p><p> Eee equals em-see-squared...<br/></p><p> <span class="sp-syntax-punctuation"/><span class="sp-syntax-tag">p</span><span class="sp-syntax-punctuation">></span><br/></p><p> <span class="sp-syntax-punctuation"/><span class="sp-syntax-tag">article</span><span class="sp-syntax-punctuation">></span><br/></p><p> <span class="sp-syntax-punctuation">)</span><span class="sp-syntax-punctuation">;</span><br/></p><p><span class="sp-syntax-punctuation">}</span></p></code></pre> </div> </div> </div> </div> <p><!--/$--></p> <p class="whitespace-pre-wrap my-4">When React renders this component, it will see the <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><title/></code> <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><link/></code> and <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><meta/></code> tags, and automatically hoist them to the <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"/> section of document. By supporting these metadata tags natively, we’re able to ensure they work with client-only apps, streaming SSR, and Server Components.</p> <div class="expandable-callout pt-8 pb-4 px-5 sm:px-8 my-8 relative rounded-none shadow-inner-border -mx-5 sm:mx-auto sm:rounded-2xl bg-green-5 dark:bg-green-60 dark:bg-opacity-20 text-primary dark:text-primary-dark text-lg"> <h3 class="text-2xl font-display font-bold text-green-60 dark:text-green-40"><span class="ez-toc-section" id="Note-2"></span><svg xmlns="http://www.w3.org/2000/svg" class="inline me-2 mb-1 text-lg text-green-60 dark:text-green-40" width="2em" height="2em" viewbox="0 0 72 72" fill="none"><g clip-path="url(#clip0_40_48064)"><path d="M24 27C24 25.3431 25.3431 24 27 24H45C46.6569 24 48 25.3431 48 27C48 28.6569 46.6569 30 45 30H27C25.3431 30 24 28.6569 24 27Z" fill="currentColor"/><path d="M24 39C24 37.3431 25.3431 36 27 36H39C40.6569 36 42 37.3431 42 39C42 40.6569 40.6569 42 39 42H27C25.3431 42 24 40.6569 24 39Z" fill="currentColor"/><path fill-rule="evenodd" clip-rule="evenodd" d="M12 18C12 13.0294 16.0294 9 21 9H51C55.9706 9 60 13.0294 60 18V54C60 58.9706 55.9706 63 51 63H21C16.0294 63 12 58.9706 12 54V18ZM21 15H51C52.6569 15 54 16.3431 54 18V54C54 55.6569 52.6569 57 51 57H21C19.3431 57 18 55.6569 18 54V18C18 16.3431 19.3431 15 21 15Z" fill="currentColor"/></g><defs><clippath id="clip0_40_48064"><rect width="72" height="72" fill="white"/></clippath></defs></svg>Note<span class="ez-toc-section-end"></span></h3> <div class="relative"> <div class="py-2"> <h4 id="you-may-still-want-a-metadata-library" class="mdx-heading text-xl font-display font-bold leading-9 my-4"><span class="ez-toc-section" id="You_may_still_want_a_Metadata_library"></span>You may still want a Metadata library <span class="ez-toc-section-end"></span></h4> <p class="whitespace-pre-wrap my-4">For simple use cases, rendering Document Metadata as tags may be suitable, but libraries can offer more powerful features like overriding generic metadata with specific metadata based on the current route. These features make it easier for frameworks and libraries like <a href="https://github.com/nfl/react-helmet" target="_blank" rel="nofollow noopener noreferrer" class="inline text-link dark:text-link-dark border-b border-link border-opacity-0 hover:border-opacity-100 duration-100 ease-in transition leading-normal"><code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">react-helmet</code></a> to support metadata tags, rather than replace them.</p> </div> </div> </div> <p class="whitespace-pre-wrap my-4">For more info, see the docs for <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><title/></code>, <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><link/></code>, and <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><meta/></code>.</p> <h3 id="support-for-stylesheets" class="mdx-heading text-2xl font-display leading-9 text-primary dark:text-primary-dark font-bold my-6"><span class="ez-toc-section" id="Support_for_stylesheets"></span>Support for stylesheets <span class="ez-toc-section-end"></span></h3> <p class="whitespace-pre-wrap my-4">Stylesheets, both externally linked (<code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><link rel=""stylesheet"" href=""...""/></code>) and inline (<code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"/>), require careful positioning in the DOM due to style precedence rules. Building a stylesheet capability that allows for composability within components is hard, so users often end up either loading all of their styles far from the components that may depend on them, or they use a style library which encapsulates this complexity.</p> <p class="whitespace-pre-wrap my-4">In React 19, we’re addressing this complexity and providing even deeper integration into Concurrent Rendering on the Client and Streaming Rendering on the Server with built in support for stylesheets. If you tell React the <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">precedence</code> of your stylesheet it will manage the insertion order of the stylesheet in the DOM and ensure that the stylesheet (if external) is loaded before revealing content that depends on those style rules.</p> <p><!--$--></p> <div dir="ltr" class="sandpack sandpack--codeblock rounded-2xl h-full w-full overflow-x-auto flex items-center bg-wash dark:bg-gray-95 shadow-lg my-8" style="contain:content"> <div class="sp-wrapper"> <div class="sp-stack"> <div class="sp-code-editor"> <pre class="sp-cm sp-pristine sp-javascript flex align-start"><code class="sp-pre-placeholder grow-[2]"><p><span class="sp-syntax-keyword">function</span> <span class="sp-syntax-definition">ComponentOne</span><span class="sp-syntax-punctuation">(</span><span class="sp-syntax-punctuation">)</span> <span class="sp-syntax-punctuation">{</span><br/></p><p> <span class="sp-syntax-keyword">return</span> <span class="sp-syntax-punctuation">(</span><br/></p><p> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-definition">Suspense</span> <span class="sp-syntax-property">fallback</span>=<span class="sp-syntax-string">"loading..."</span><span class="sp-syntax-punctuation">></span><br/></p><p> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-tag">link</span> <span class="sp-syntax-property">rel</span>=<span class="sp-syntax-string">"stylesheet"</span> <span class="sp-syntax-property">href</span>=<span class="sp-syntax-string">"foo"</span> <span class="sp-syntax-property">precedence</span>=<span class="sp-syntax-string">"default"</span> <span class="sp-syntax-punctuation">/></span><br/></p><p> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-tag">link</span> <span class="sp-syntax-property">rel</span>=<span class="sp-syntax-string">"stylesheet"</span> <span class="sp-syntax-property">href</span>=<span class="sp-syntax-string">"bar"</span> <span class="sp-syntax-property">precedence</span>=<span class="sp-syntax-string">"high"</span> <span class="sp-syntax-punctuation">/></span><br/></p><p> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-tag">article</span> <span class="sp-syntax-property">class</span>=<span class="sp-syntax-string">"foo-class bar-class"</span><span class="sp-syntax-punctuation">></span><br/></p><p> <span class="sp-syntax-punctuation">{</span><span class="sp-syntax-punctuation">...</span>}<br/></p><p> <span class="sp-syntax-punctuation"/><span class="sp-syntax-tag">article</span><span class="sp-syntax-punctuation">></span><br/></p><p> <span class="sp-syntax-punctuation"/><span class="sp-syntax-definition">Suspense</span><span class="sp-syntax-punctuation">></span><br/></p><p> <span class="sp-syntax-punctuation">)</span><br/></p><p><span class="sp-syntax-punctuation">}</span><br/></p><p><span class="sp-syntax-keyword">function</span> <span class="sp-syntax-definition">ComponentTwo</span><span class="sp-syntax-punctuation">(</span><span class="sp-syntax-punctuation">)</span> <span class="sp-syntax-punctuation">{</span><br/></p><p> <span class="sp-syntax-keyword">return</span> <span class="sp-syntax-punctuation">(</span><br/></p><p> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-tag">div</span><span class="sp-syntax-punctuation">></span><br/></p><p> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-tag">p</span><span class="sp-syntax-punctuation">></span><span class="sp-syntax-punctuation">{</span><span class="sp-syntax-punctuation">...</span>}<span class="sp-syntax-punctuation"/><span class="sp-syntax-tag">p</span><span class="sp-syntax-punctuation">></span><br/></p><p> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-tag">link</span> <span class="sp-syntax-property">rel</span>=<span class="sp-syntax-string">"stylesheet"</span> <span class="sp-syntax-property">href</span>=<span class="sp-syntax-string">"baz"</span> <span class="sp-syntax-property">precedence</span>=<span class="sp-syntax-string">"default"</span> <span class="sp-syntax-punctuation">/></span> <span class="sp-syntax-punctuation"><</span>-- will be inserted between foo & bar<br/></p><p> <span class="sp-syntax-punctuation"/><span class="sp-syntax-tag">div</span><span class="sp-syntax-punctuation">></span><br/></p><p> )<br/></p><p>}</p></code></pre> </div> </div> </div> </div> <p><!--/$--></p> <p class="whitespace-pre-wrap my-4">During Server Side Rendering React will include the stylesheet in the <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"/>, which ensures that the browser will not paint until it has loaded. If the stylesheet is discovered late after we’ve already started streaming, React will ensure that the stylesheet is inserted into the <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"/> on the client before revealing the content of a Suspense boundary that depends on that stylesheet.</p> <p class="whitespace-pre-wrap my-4">During Client Side Rendering React will wait for newly rendered stylesheets to load before committing the render. If you render this component from multiple places within your application React will only include the stylesheet once in the document:</p> <p><!--$--></p> <div dir="ltr" class="sandpack sandpack--codeblock rounded-2xl h-full w-full overflow-x-auto flex items-center bg-wash dark:bg-gray-95 shadow-lg my-8" style="contain:content"> <div class="sp-wrapper"> <div class="sp-stack"> <div class="sp-code-editor"> <pre class="sp-cm sp-pristine sp-javascript flex align-start"><code class="sp-pre-placeholder grow-[2]"><p><span class="sp-syntax-keyword">function</span> <span class="sp-syntax-definition">App</span><span class="sp-syntax-punctuation">(</span><span class="sp-syntax-punctuation">)</span> <span class="sp-syntax-punctuation">{</span><br/></p><p> <span class="sp-syntax-keyword">return</span> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-punctuation">></span><br/></p><p> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-definition">ComponentOne</span> <span class="sp-syntax-punctuation">/></span><br/></p><p> ...<br/></p><p> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-definition">ComponentOne</span> <span class="sp-syntax-punctuation">/></span> // won't lead to a duplicate stylesheet link in the DOM<br/></p><p> <span class="sp-syntax-punctuation"/><span class="sp-syntax-punctuation">></span><br/></p><p><span class="sp-syntax-punctuation">}</span></p></code></pre> </div> </div> </div> </div> <p><!--/$--></p> <p class="whitespace-pre-wrap my-4">For users accustomed to loading stylesheets manually this is an opportunity to locate those stylesheets alongside the components that depend on them allowing for better local reasoning and an easier time ensuring you only load the stylesheets that you actually depend on.</p> <p class="whitespace-pre-wrap my-4">Style libraries and style integrations with bundlers can also adopt this new capability so even if you don’t directly render your own stylesheets, you can still benefit as your tools are upgraded to use this feature.</p> <p class="whitespace-pre-wrap my-4">For more details, read the docs for <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><link/></code> and <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"/>.</p> <h3 id="support-for-async-scripts" class="mdx-heading text-2xl font-display leading-9 text-primary dark:text-primary-dark font-bold my-6"><span class="ez-toc-section" id="Support_for_async_scripts"></span>Support for async scripts <span class="ez-toc-section-end"></span></h3> <p class="whitespace-pre-wrap my-4">In HTML normal scripts (<code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><script src=""...""/></code>) and deferred scripts (<code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><script defer="""" src=""...""/></code>) load in document order which makes rendering these kinds of scripts deep within your component tree challenging. Async scripts (<code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><script async="""" src=""...""/></code>) however will load in arbitrary order. <p class="whitespace-pre-wrap my-4">In React 19 we’ve included better support for async scripts by allowing you to render them anywhere in your component tree, inside the components that actually depend on the script, without having to manage relocating and deduplicating script instances. <!--$--> <div dir="ltr" class="sandpack sandpack--codeblock rounded-2xl h-full w-full overflow-x-auto flex items-center bg-wash dark:bg-gray-95 shadow-lg my-8" style="contain:content"> <div class="sp-wrapper"> <div class="sp-stack"> <div class="sp-code-editor"> <pre class="sp-cm sp-pristine sp-javascript flex align-start"><code class="sp-pre-placeholder grow-[2]"><span class="sp-syntax-keyword">function</span> <span class="sp-syntax-definition">MyComponent</span><span class="sp-syntax-punctuation">(</span><span class="sp-syntax-punctuation">)</span> <span class="sp-syntax-punctuation">{</span><br/> <span class="sp-syntax-keyword">return</span> <span class="sp-syntax-punctuation">(</span><br/> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-tag">div</span><span class="sp-syntax-punctuation">></span><br/> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-tag">script</span> <span class="sp-syntax-property">async</span>=<span class="sp-syntax-punctuation">{</span><span class="sp-syntax-static">true</span><span class="sp-syntax-punctuation">}</span> <span class="sp-syntax-property">src</span>=<span class="sp-syntax-string">"..."</span> <span class="sp-syntax-punctuation">/></span><br/> Hello World<br/> <span class="sp-syntax-punctuation"/><span class="sp-syntax-tag">div</span><span class="sp-syntax-punctuation">></span><br/> <span class="sp-syntax-punctuation">)</span><br/><span class="sp-syntax-punctuation">}</span><br/><span class="sp-syntax-keyword">function</span> <span class="sp-syntax-definition">App</span><span class="sp-syntax-punctuation">(</span><span class="sp-syntax-punctuation">)</span> <span class="sp-syntax-punctuation">{</span><br/> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-tag">html</span><span class="sp-syntax-punctuation">></span><br/> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-tag">body</span><span class="sp-syntax-punctuation">></span><br/> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-definition">MyComponent</span><span class="sp-syntax-punctuation">></span><br/> ...<br/> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-definition">MyComponent</span><span class="sp-syntax-punctuation">></span> // won't lead to duplicate script in the DOM<br/> <span class="sp-syntax-punctuation"/><span class="sp-syntax-tag">body</span><span class="sp-syntax-punctuation">></span><br/> <span class="sp-syntax-punctuation"/><span class="sp-syntax-tag">html</span><span class="sp-syntax-punctuation">></span><br/>}</code></pre> </div> </div> </div> </div> <!--/$--> <p class="whitespace-pre-wrap my-4">In all rendering environments, async scripts will be deduplicated so that React will only load and execute the script once even if it is rendered by multiple different components. <p class="whitespace-pre-wrap my-4">In Server Side Rendering, async scripts will be included in the <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"/> and prioritized behind more critical resources that block paint such as stylesheets, fonts, and image preloads. <p class="whitespace-pre-wrap my-4">For more details, read the docs for <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><script/></code>. <h3 id="support-for-preloading-resources" class="mdx-heading text-2xl font-display leading-9 text-primary dark:text-primary-dark font-bold my-6"><span class="ez-toc-section" id="Support_for_preloading_resources"></span>Support for preloading resources <span class="ez-toc-section-end"></span></h3> <p class="whitespace-pre-wrap my-4">During initial document load and on client side updates, telling the Browser about resources that it will likely need to load as early as possible can have a dramatic effect on page performance. <p class="whitespace-pre-wrap my-4">React 19 includes a number of new APIs for loading and preloading Browser resources to make it as easy as possible to build great experiences that aren’t held back by inefficient resource loading. <!--$--> <div dir="ltr" class="sandpack sandpack--codeblock rounded-2xl h-full w-full overflow-x-auto flex items-center bg-wash dark:bg-gray-95 shadow-lg my-8" style="contain:content"> <div class="sp-wrapper"> <div class="sp-stack"> <div class="sp-code-editor"> <pre class="sp-cm sp-pristine sp-javascript flex align-start"><code class="sp-pre-placeholder grow-[2]"><span class="sp-syntax-keyword">import</span> <span class="sp-syntax-punctuation">{</span> <span class="sp-syntax-plain">prefetchDNS</span><span class="sp-syntax-punctuation">,</span> <span class="sp-syntax-plain">preconnect</span><span class="sp-syntax-punctuation">,</span> <span class="sp-syntax-plain">preload</span><span class="sp-syntax-punctuation">,</span> <span class="sp-syntax-plain">preinit</span> <span class="sp-syntax-punctuation">}</span> <span class="sp-syntax-keyword">from</span> <span class="sp-syntax-string">'react-dom'</span><br/><span class="sp-syntax-keyword">function</span> <span class="sp-syntax-definition">MyComponent</span><span class="sp-syntax-punctuation">(</span><span class="sp-syntax-punctuation">)</span> <span class="sp-syntax-punctuation">{</span><br/> <span class="sp-syntax-definition">preinit</span><span class="sp-syntax-punctuation">(</span><span class="sp-syntax-string">'https://.../path/to/some/script.js'</span><span class="sp-syntax-punctuation">,</span> <span class="sp-syntax-punctuation">{</span><span class="sp-syntax-property">as</span><span class="sp-syntax-punctuation">:</span> <span class="sp-syntax-string">'script'</span> <span class="sp-syntax-punctuation">}</span><span class="sp-syntax-punctuation">)</span> <br/> <span class="sp-syntax-definition">preload</span><span class="sp-syntax-punctuation">(</span><span class="sp-syntax-string">'https://.../path/to/font.woff'</span><span class="sp-syntax-punctuation">,</span> <span class="sp-syntax-punctuation">{</span> <span class="sp-syntax-property">as</span><span class="sp-syntax-punctuation">:</span> <span class="sp-syntax-string">'font'</span> <span class="sp-syntax-punctuation">}</span><span class="sp-syntax-punctuation">)</span> <br/> <span class="sp-syntax-definition">preload</span><span class="sp-syntax-punctuation">(</span><span class="sp-syntax-string">'https://.../path/to/stylesheet.css'</span><span class="sp-syntax-punctuation">,</span> <span class="sp-syntax-punctuation">{</span> <span class="sp-syntax-property">as</span><span class="sp-syntax-punctuation">:</span> <span class="sp-syntax-string">'style'</span> <span class="sp-syntax-punctuation">}</span><span class="sp-syntax-punctuation">)</span> <br/> <span class="sp-syntax-definition">prefetchDNS</span><span class="sp-syntax-punctuation">(</span><span class="sp-syntax-string">'https://...'</span><span class="sp-syntax-punctuation">)</span> <br/> <span class="sp-syntax-definition">preconnect</span><span class="sp-syntax-punctuation">(</span><span class="sp-syntax-string">'https://...'</span><span class="sp-syntax-punctuation">)</span> <br/><span class="sp-syntax-punctuation">}</span></code></pre> </div> </div> </div> </div> <!--/$--> <!--$--> <div dir="ltr" class="sandpack sandpack--codeblock rounded-2xl h-full w-full overflow-x-auto flex items-center bg-wash dark:bg-gray-95 shadow-lg my-8" style="contain:content"> <div class="sp-wrapper"> <div class="sp-stack"> <div class="sp-code-editor"> <pre class="sp-cm sp-pristine sp-javascript flex align-start"><code class="sp-pre-placeholder grow-[2]"><span class="sp-syntax-punctuation"><</span><span class="sp-syntax-definition">html</span><span class="sp-syntax-punctuation">></span><br/> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-definition">head</span><span class="sp-syntax-punctuation">></span><br/> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-definition">link</span> <span class="sp-syntax-property">rel</span>=<span class="sp-syntax-string">"prefetch-dns"</span> <span class="sp-syntax-property">href</span>=<span class="sp-syntax-string">"https://..."</span><span class="sp-syntax-punctuation">></span><br/> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-definition">link</span> <span class="sp-syntax-property">rel</span>=<span class="sp-syntax-string">"preconnect"</span> <span class="sp-syntax-property">href</span>=<span class="sp-syntax-string">"https://..."</span><span class="sp-syntax-punctuation">></span><br/> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-definition">link</span> <span class="sp-syntax-property">rel</span>=<span class="sp-syntax-string">"preload"</span> <span class="sp-syntax-property">as</span>=<span class="sp-syntax-string">"font"</span> <span class="sp-syntax-property">href</span>=<span class="sp-syntax-string">"https://.../path/to/font.woff"</span><span class="sp-syntax-punctuation">></span><br/> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-definition">link</span> <span class="sp-syntax-property">rel</span>=<span class="sp-syntax-string">"preload"</span> <span class="sp-syntax-property">as</span>=<span class="sp-syntax-string">"style"</span> <span class="sp-syntax-property">href</span>=<span class="sp-syntax-string">"https://.../path/to/stylesheet.css"</span><span class="sp-syntax-punctuation">></span><br/> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-definition">script</span> <span class="sp-syntax-property">async</span>=<span class="sp-syntax-string">""</span> <span class="sp-syntax-property">src</span>=<span class="sp-syntax-string">"https://.../path/to/some/script.js"</span><span class="sp-syntax-punctuation">></span><span class="sp-syntax-punctuation"/><span class="sp-syntax-definition">script</span><span class="sp-syntax-punctuation">></span><br/> <span class="sp-syntax-punctuation"/><span class="sp-syntax-definition">head</span><span class="sp-syntax-punctuation">></span><br/> <span class="sp-syntax-punctuation"><</span><span class="sp-syntax-definition">body</span><span class="sp-syntax-punctuation">></span><br/> ...<br/> <span class="sp-syntax-punctuation"/><span class="sp-syntax-definition">body</span><span class="sp-syntax-punctuation">></span><br/><span class="sp-syntax-punctuation"/><span class="sp-syntax-definition">html</span><span class="sp-syntax-punctuation">></span></code></pre> </div> </div> </div> </div> <!--/$--> <p class="whitespace-pre-wrap my-4">These APIs can be used to optimize initial page loads by moving discovery of additional resources like fonts out of stylesheet loading. They can also make client updates faster by prefetching a list of resources used by an anticipated navigation and then eagerly preloading those resources on click or even on hover. <p class="whitespace-pre-wrap my-4">For more details see Resource Preloading APIs. <h3 id="compatibility-with-third-party-scripts-and-extensions" class="mdx-heading text-2xl font-display leading-9 text-primary dark:text-primary-dark font-bold my-6"><span class="ez-toc-section" id="Compatibility_with_third-party_scripts_and_extensions"></span>Compatibility with third-party scripts and extensions <span class="ez-toc-section-end"></span></h3> <p class="whitespace-pre-wrap my-4">We’ve improved hydration to account for third-party scripts and browser extensions. <p class="whitespace-pre-wrap my-4">When hydrating, if an element that renders on the client doesn’t match the element found in the HTML from the server, React will force a client re-render to fix up the content. Previously, if an element was inserted by third-party scripts or browser extensions, it would trigger a mismatch error and client render. <p class="whitespace-pre-wrap my-4">In React 19, unexpected tags in the <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"/> and <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"/> will be skipped over, avoiding the mismatch errors. If React needs to re-render the entire document due to an unrelated hydration mismatch, it will leave in place stylesheets inserted by third-party scripts and browser extensions. <h3 id="error-handling" class="mdx-heading text-2xl font-display leading-9 text-primary dark:text-primary-dark font-bold my-6"><span class="ez-toc-section" id="Better_error_reporting"></span>Better error reporting <span class="ez-toc-section-end"></span></h3> <p class="whitespace-pre-wrap my-4">We improved error handling in React 19 to remove duplication and provide options for handling caught and uncaught errors. For example, when there’s an error in render caught by an Error Boundary, previously React would throw the error twice (once for the original error, then again after failing to automatically recover), and then call <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">console.error</code> with info about where the error occurred. <p class="whitespace-pre-wrap my-4">This resulted in three errors for every caught error: <div class="console-block mb-4 text-secondary bg-wash dark:bg-wash-dark rounded-lg" translate="no" dir="ltr"> <div class="grid grid-cols-1 divide-y divide-gray-300 dark:divide-gray-70 text-base"> <div class="ps-4 pe-2 pt-1 pb-2 grid grid-cols-[18px_auto] font-mono rounded-b-md bg-red-30 text-red-50 dark:text-red-30 bg-opacity-5"><svg xmlns="http://www.w3.org/2000/svg" class="self-start mt-1.5 text-[.7rem] w-6" width="1.33em" height="1.33em" viewbox="0 0 24 24"><circle cx="10.1626" cy="9.99951" r="9.47021" fill="currentColor"/><path d="M6.22705 5.95996L14.2798 14.0127" stroke="white"/><path d="M14.2798 5.95996L6.22705 14.0127" stroke="white"/></svg> Uncaught Error: hit <!-- --> <!-- -->at Throws <!-- --> <!-- -->at renderWithHooks <!-- --> <!-- -->… </div> <div class="ps-4 pe-2 pt-1 pb-2 grid grid-cols-[18px_auto] font-mono rounded-b-md bg-red-30 text-red-50 dark:text-red-30 bg-opacity-5"><svg xmlns="http://www.w3.org/2000/svg" class="self-start mt-1.5 text-[.7rem] w-6" width="1.33em" height="1.33em" viewbox="0 0 24 24"><circle cx="10.1626" cy="9.99951" r="9.47021" fill="currentColor"/><path d="M6.22705 5.95996L14.2798 14.0127" stroke="white"/><path d="M14.2798 5.95996L6.22705 14.0127" stroke="white"/></svg> Uncaught Error: hit<span class="ms-2 text-gray-30"> <--<!-- --> Duplicate</span> <!-- --> <!-- -->at Throws <!-- --> <!-- -->at renderWithHooks <!-- --> <!-- -->… </div> <div class="ps-4 pe-2 pt-1 pb-2 grid grid-cols-[18px_auto] font-mono rounded-b-md bg-red-30 text-red-50 dark:text-red-30 bg-opacity-5"><svg xmlns="http://www.w3.org/2000/svg" class="self-start mt-1.5 text-[.7rem] w-6" width="1.33em" height="1.33em" viewbox="0 0 24 24"><circle cx="10.1626" cy="9.99951" r="9.47021" fill="currentColor"/><path d="M6.22705 5.95996L14.2798 14.0127" stroke="white"/><path d="M14.2798 5.95996L6.22705 14.0127" stroke="white"/></svg> The above error occurred in the Throws component: <!-- --> <!-- -->at Throws <!-- --> <!-- -->at ErrorBoundary <!-- --> <!-- -->at App<!-- --> <!-- --> React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary. </div> </div> </div> <p class="whitespace-pre-wrap my-4">In React 19, we log a single error with all the error information included: <div class="console-block mb-4 text-secondary bg-wash dark:bg-wash-dark rounded-lg" translate="no" dir="ltr"> <div class="grid grid-cols-1 divide-y divide-gray-300 dark:divide-gray-70 text-base"> <div class="ps-4 pe-2 pt-1 pb-2 grid grid-cols-[18px_auto] font-mono rounded-b-md bg-red-30 text-red-50 dark:text-red-30 bg-opacity-5"><svg xmlns="http://www.w3.org/2000/svg" class="self-start mt-1.5 text-[.7rem] w-6" width="1.33em" height="1.33em" viewbox="0 0 24 24"><circle cx="10.1626" cy="9.99951" r="9.47021" fill="currentColor"/><path d="M6.22705 5.95996L14.2798 14.0127" stroke="white"/><path d="M14.2798 5.95996L6.22705 14.0127" stroke="white"/></svg> Error: hit <!-- --> <!-- -->at Throws <!-- --> <!-- -->at renderWithHooks <!-- --> <!-- -->…<!-- --> <!-- --> The above error occurred in the Throws component: <!-- --> <!-- -->at Throws <!-- --> <!-- -->at ErrorBoundary <!-- --> <!-- -->at App<!-- --> <!-- --> React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary. <!-- --> <!-- -->at ErrorBoundary <!-- --> <!-- -->at App </div> </div> </div> <p class="whitespace-pre-wrap my-4">Additionally, we’ve added two new root options to complement <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">onRecoverableError</code>: <ul class="ms-6 my-3 list-disc"> <li class="leading-relaxed mb-1"><code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">onCaughtError</code>: called when React catches an error in an Error Boundary.</li> <li class="leading-relaxed mb-1"><code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">onUncaughtError</code>: called when an error is thrown and not caught by an Error Boundary.</li> <li class="leading-relaxed mb-1"><code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">onRecoverableError</code>: called when an error is thrown and automatically recovered.</li> </ul> <p class="whitespace-pre-wrap my-4">For more info and examples, see the docs for <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">createRoot</code> and <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">hydrateRoot</code>. <h3 id="support-for-custom-elements" class="mdx-heading text-2xl font-display leading-9 text-primary dark:text-primary-dark font-bold my-6"><span class="ez-toc-section" id="Support_for_Custom_Elements"></span>Support for Custom Elements <span class="ez-toc-section-end"></span></h3> <p class="whitespace-pre-wrap my-4">React 19 adds full support for custom elements and passes all tests on <a href="https://custom-elements-everywhere.com/" target="_blank" rel="nofollow noopener noreferrer" class="inline text-link dark:text-link-dark border-b border-link border-opacity-0 hover:border-opacity-100 duration-100 ease-in transition leading-normal">Custom Elements Everywhere</a>. <p class="whitespace-pre-wrap my-4">In past versions, using Custom Elements in React has been difficult because React treated unrecognized props as attributes rather than properties. In React 19, we’ve added support for properties that works on the client and during SSR with the following strategy: <ul class="ms-6 my-3 list-disc"> <li class="leading-relaxed mb-1"><strong class="font-bold">Server Side Rendering</strong>: props passed to a custom element will render as attributes if their type is a primitive value like <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">string</code>, <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">number</code>, or the value is <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">true</code>. Props with non-primitive types like <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">object</code>, <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">symbol</code>, <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">function</code>, or value <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">false</code> will be omitted.</li> <li class="leading-relaxed mb-1"><strong class="font-bold">Client Side Rendering</strong>: props that match a property on the Custom Element instance will be assigned as properties, otherwise they will be assigned as attributes.</li> </ul> <p class="whitespace-pre-wrap my-4">Thanks to <a href="https://github.com/josepharhar" target="_blank" rel="nofollow noopener noreferrer" class="inline text-link dark:text-link-dark border-b border-link border-opacity-0 hover:border-opacity-100 duration-100 ease-in transition leading-normal">Joey Arhar</a> for driving the design and implementation of Custom Element support in React. <h4 id="how-to-upgrade" class="mdx-heading text-xl font-display font-bold leading-9 my-4"><span class="ez-toc-section" id="How_to_upgrade"></span>How to upgrade <span class="ez-toc-section-end"></span></h4> <p class="whitespace-pre-wrap my-4">See the React 19 Upgrade Guide for step-by-step instructions and a full list of breaking and notable changes. <p class="whitespace-pre-wrap my-4"><em>Note: this post was originally published 04/25/2024 and has been updated to 12/05/2024 with the stable release.</em> </div> <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script></p> <!-- CONTENT END 2 --> </div><!-- .entry-content --> <footer class="entry-footer"> <div class="entry-tags"> <span class="tags-links"> <span class="tags-label screen-reader-text"> Post Tags: </span> <a href=https://reactjsnotes.online/tag/react-development/ title="React Development" class="tag-link tag-item-react-development" rel="tag"><span class="tag-hash">#</span>React Development</a> </span> </div><!-- .entry-tags --> </footer><!-- .entry-footer --> </div> </article><!-- #post-15481 --> <nav class="navigation post-navigation" aria-label="Posts"> <h2 class="screen-reader-text">Post navigation</h2> <div class="nav-links"><div class="nav-previous"><a href="https://reactjsnotes.online/how-nvidias-inference-software-stack-powers-the-lowest-token-cost/" rel="prev"><div class="post-navigation-sub"><small><span class="kadence-svg-iconset svg-baseline"><svg aria-hidden="true" class="kadence-svg-icon kadence-arrow-left-alt-svg" fill="currentColor" version="1.1" xmlns="http://www.w3.org/2000/svg" width="29" height="28" viewBox="0 0 29 28"><title>Previous Previous

How NVIDIA’s Inference Software Stack Powers the Lowest Token Cost

Similar Posts

Leave a Reply