🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Components & Islands••By Komal Nakrani

React in Astro: How to Use React Components

Use React in Astro: install @astrojs/react, render components, hydrate them with client directives, share state between islands, and avoid the common pitfalls.

#Astro#React in Astro#React#Islands#Integrations

To use React in Astro, run npx astro add react, import a .jsx or .tsx component into an .astro file, and render it like any other component. It renders to static HTML by default; add a client directive such as client:load or client:visible when it needs to be interactive in the browser. Each hydrated React component becomes an independent island, so the rest of the page stays free of React.

This guide covers setup, hydration, props and children, sharing state between islands, mixing React with other frameworks, and the errors you’re likely to hit, all on Astro 7.

Key Takeaways

  • npx astro add react installs @astrojs/react, react and react-dom, and updates astro.config.mjs and tsconfig.json.
  • React components render to HTML with zero JavaScript unless you add a client:* directive.
  • Hooks and event handlers only run in hydrated islands.
  • React context doesn’t cross island boundaries; share state with a store like nanostores.
  • You can’t import .astro components inside React, but you can pass them in as children from an Astro file.

Add the React integration

From your project root:

# terminal
npx astro add react

The command installs the packages and makes two edits. In astro.config.mjs:

// astro.config.mjs
import { defineConfig } from 'astro/config';
import react from '@astrojs/react';

export default defineConfig({
  integrations: [react()],
});

And in tsconfig.json, it sets the JSX options React needs:

{
  "extends": "astro/tsconfigs/strict",
  "include": [".astro/types.d.ts", "**/*"],
  "exclude": ["dist"],
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "react"
  }
}

If you’d rather install manually, add @astrojs/react, react, react-dom and (for TypeScript) @types/react and @types/react-dom, then make the same two edits.

Render a React component in Astro

Write a normal React component:

// src/components/Counter.tsx
import { useState } from 'react';

interface Props {
  start?: number;
  label: string;
}

export default function Counter({ start = 0, label }: Props) {
  const [count, setCount] = useState(start);
  return (
    <button type="button" onClick={() => setCount((c) => c + 1)}>
      {label}: {count}
    </button>
  );
}

Import and use it in an Astro page:

---
// src/pages/index.astro
import BaseLayout from '../layouts/BaseLayout.astro';
import Counter from '../components/Counter.tsx';
---
<BaseLayout title="Home">
  <h1>Votes</h1>
  <Counter label="Static" />
  <Counter client:visible label="Interactive" start={5} />
</BaseLayout>

Both counters render a button in the HTML. The first stays static: clicking does nothing, because no JavaScript was sent for it. The second hydrates when it scrolls into view, loading React and the component, and then works.

This is the most common confusion for React developers new to Astro: React components in Astro are server-rendered templates by default. You opt into browser React per component.

Choose a client directive

Directive Use for
none Components that only render markup: cards, lists, formatted content
client:load Above-the-fold controls needed immediately
client:idle Lower-priority widgets that should work soon
client:visible Anything below the fold
client:media="(max-width: 768px)" Viewport-specific UI
client:only="react" Components that can’t render on the server

Our comparison of Astro client directives explains each one and how to choose.

Using React without a directive is a legitimate pattern, not a mistake. If your team has a React component library, you can reuse its presentational components in Astro pages and ship no React to the browser for them.

Props and serialization

Props passed to a hydrated island are serialized into the HTML so React can rebuild them in the browser. Strings, numbers, booleans, arrays, plain objects, Date, Map, Set and URL work. Functions are dropped:

---
// src/pages/index.astro
import Counter from '../components/Counter.tsx';
---
<!-- onChange won't reach the browser -->
<Counter client:load label="Votes" onChange={(n) => console.log(n)} />

Handle events inside the React component. If something outside needs to know about them, use a shared store or dispatch a DOM event. Also keep props small: a 2 MB JSON blob passed to an island is embedded in the page HTML.

Children and named slots

You can pass content from Astro into a React component. The default slot arrives as children; named slots arrive as props:

---
// src/pages/index.astro
import Accordion from '../components/Accordion.tsx';
import Badge from '../components/Badge.astro';
---
<Accordion client:visible>
  <span slot="summary">Shipping details <Badge text="New" /></span>
  <p>Orders leave within two business days.</p>
</Accordion>
// src/components/Accordion.tsx
import { useState, type ReactNode } from 'react';

export default function Accordion({ summary, children }: { summary: ReactNode; children: ReactNode }) {
  const [open, setOpen] = useState(false);
  return (
    <div>
      <button type="button" aria-expanded={open} onClick={() => setOpen(!open)}>
        {summary}
      </button>
      {open && <div>{children}</div>}
    </div>
  );
}

Astro renders the slot content (including the Astro Badge component) to static HTML on the server and passes it to React. Kebab-case slot names become camelCase props, so slot="social-links" arrives as socialLinks.

What you can’t do is the reverse: import an .astro file inside a .tsx file. Astro components only render on the server, inside Astro’s own templates. If React needs a piece of UI, write that piece in React.

Hooks, context and state across islands

Inside a hydrated island, React works normally: useState, useEffect, useReducer, context providers, custom hooks. The limits are at the island boundary.

Each island is a separate React root. A context provider in one island doesn’t reach another, and wrapping the Astro layout in a provider has no effect because Astro templates aren’t React. To share state between islands, use a framework-agnostic store. nanostores is small and has React bindings:

# terminal
npm install nanostores @nanostores/react
// src/stores/cart.ts
import { atom } from 'nanostores';

export const cartCount = atom(0);
// src/components/AddToCart.tsx
import { cartCount } from '../stores/cart';

export default function AddToCart() {
  return (
    <button type="button" onClick={() => cartCount.set(cartCount.get() + 1)}>
      Add to cart
    </button>
  );
}
// src/components/CartBadge.tsx
import { useStore } from '@nanostores/react';
import { cartCount } from '../stores/cart';

export default function CartBadge() {
  const count = useStore(cartCount);
  return <span aria-live="polite">{count}</span>;
}

Put <CartBadge client:load /> in the header and <AddToCart client:visible /> on the product, and they stay in sync. The same store works from Vue, Svelte and Solid islands.

If you find yourself sharing state across many islands, consider merging them into one larger React island for that part of the page.

React with other frameworks

Astro can render React, Preact, Solid, Vue and Svelte on the same page. React, Preact and Solid all use JSX, so if you install more than one, tell each integration which files are its own:

// astro.config.mjs
import { defineConfig } from 'astro/config';
import react from '@astrojs/react';
import preact from '@astrojs/preact';

export default defineConfig({
  integrations: [
    react({ include: ['**/react/**'] }),
    preact({ include: ['**/preact/**'] }),
  ],
});

Then keep components in src/components/react/ and src/components/preact/. Vue and Svelte use their own file extensions, so they need no include. Our article on Svelte and Vue in Astro covers those integrations.

Common errors with React in Astro

“No matching renderer found.” Astro found a .jsx or .tsx component but no integration claims it. Install @astrojs/react and add it to integrations. If you run several JSX frameworks, check that the file matches the include pattern. The no matching renderer fix has the full message and steps.

window is not defined during build. The component touches a browser API while rendering, which fails on the server. Move that code into useEffect, or use client:only="react" if the component truly can’t render on the server.

Buttons don’t respond. The component has no client:* directive, so it’s static HTML.

Hydration mismatch warnings. The server HTML differs from the first client render, usually because of dates, random values or localStorage read during render. Compute those values in useEffect.

When to use React in Astro, and when not to

Use React islands when you already have React components to reuse, when a widget has real state (filters, carts, multi-step forms), or when you rely on a React-only library. For small interactions like a menu toggle or a copy button, a <script> tag in an Astro component ships far less code; see island or script. And if nearly the whole page is interactive React, a React framework may be a better fit; Astro vs Next.js covers that choice.

Komal themes are built with Astro components and ship no framework runtime by default, so adding React islands to one is a matter of running npx astro add react; you can browse Astro themes and set one up with the installation docs. The official @astrojs/react guide lists every integration option.

FAQ

Can you use React components in Astro?

Yes. Install the official integration with npx astro add react, import your .jsx or .tsx components into .astro files, and add a client:* directive to any component that needs to be interactive in the browser.

Why is my React component not interactive in Astro?

It has no client directive, so Astro rendered it to static HTML and sent no JavaScript. Add client:load, client:idle or client:visible to the component where you use it.

Does Astro ship the React runtime on every page?

No. React is only loaded on pages that contain a hydrated React island. Pages with no client:* React components send no React code at all, and the runtime is shared and cached across islands that use it.

Can I use React context across Astro islands?

No. Each island is a separate React root, so a provider in one doesn’t reach another. Share state with a framework-agnostic store such as nanostores, or combine the related components into one island.

Can I use Astro components inside React components?

Not by importing them. Astro components only render on the server inside .astro files. You can pass Astro components into a React component as children or named slots from an Astro file, where they arrive as static HTML.