# preinit()

> The `preinit()` API in React DOM eagerly fetches and evaluates a stylesheet or external script.

## What is the difference between preload and preinit in React?

The API that downloads and immediately executes a script or inserts a stylesheet. Unlike `preload()`, the resource is applied as soon as it is loaded.

- Topic: APIs [React DOM]
- Type: API

## Example

Wrong:

```jsx
function Chart() {
  preinit('/styles/chart.css', { as: 'style' });
  return <canvas />;
}
```

Right:

```jsx
function Chart() {
  preinit('/styles/chart.css', { as: 'style', precedence: 'medium' });
  return <canvas />;
}
```

Stylesheets need `precedence`, which tells React where to insert them relative to other stylesheets on the page.

## Common mistakes

- Preinitializing a heavy script that is not needed yet, which blocks the main thread early.

## Key points

- `preinit` is used only for scripts and stylesheets that should run now, with `as` set to 'script' or 'style'.
- Stylesheets passed to `preinit` set `precedence` so they are inserted in a stable order.
- Resources that should only be fetched, not executed, use `preload` instead.

## Interview questions

### Does preinit run the script immediately?

Yes. `preinit` with `as: 'script'` downloads and runs the script as soon as it is ready, like an async script tag.

### Is there a preinit for ES modules?

Yes. Use `preinitModule` for module scripts, and `preloadModule` when the module should only be downloaded.

## Related cards

- [preload()](https://wonderium.dev/react/preload.md)
- [preconnect()](https://wonderium.dev/react/preconnect.md)
- [\[rel="modulepreload"\]](https://wonderium.dev/html/rel-modulepreload.md)
- [`<script>`](https://wonderium.dev/html/script.md)
- [LCP](https://wonderium.dev/web/lcp.md)

---

- Level: [React](https://wonderium.dev/react.md)
- HTML version: https://wonderium.dev/react/preinit
- Last updated: 2026-11-25
