Start required resource work early
A component and its descendants are known to need a resource during rendering.
React 19+ · Function
preinit from react-dom eagerly fetches and initializes an external script or stylesheet. Scripts execute after download; stylesheets are inserted and applied immediately.
Call preinit from react-dom to tell the browser to fetch a script or stylesheet early. Scripts execute after downloading, while stylesheets are inserted into the document and take effect immediately.
preinit(href, options)| Name | Type | Description |
|---|---|---|
href | string | URL of the resource to download and execute. |
options | object | Configuration object. as is required and must be script or style; stylesheet initialization also requires precedence. It may include crossOrigin and fetchPriority. |
Nothing (undefined).
preinit from react-dom, not from the core react package.as.precedence; omitting it makes the stylesheet configuration invalid.import { preinit } from 'react-dom';
export default function AppRoot() {
preinit('https://example.com/app.js', { as: 'script' });
preinit('/theme.css', { as: 'style', precedence: 'high' });
return <main>Application</main>;
}The component eagerly fetches and evaluates the script and fetches and applies the stylesheet during rendering.
A component and its descendants are known to need a resource during rendering.
A user is about to navigate to a view that needs an external script or stylesheet.
A stylesheet must participate in a controlled cascade order.
preinit do, and what does it return?preinit is a resource-hint API imported from react-dom. It asks the browser to eagerly fetch and evaluate an external script or fetch and insert a stylesheet; it returns nothing.
Follow-up: When would you avoid it? Avoid it when immediate script execution or stylesheet application is not acceptable, or when the resource may never be needed.
The first argument is the resource URL. The options object requires as: 'script' or as: 'style'; stylesheet use also requires a precedence value.
Follow-up: What precedence values are documented? reset, low, medium, and high; higher precedence can override lower precedence.
preinit be called?Call it while rendering when the component or its children are known to need a resource. It can also be called in an event handler before transitioning to a page or state that needs external resources.
Follow-up: Why use an event handler? It lets the browser begin fetching before the anticipated transition occurs.
preloadpreloadModulepreinitModuleprefetchDNSpreconnect