React components let you build interactive parts of a website. Using them in Astro helps combine React's power with Astro's fast page loading.
React components in Astro
import ReactComponent from './Component.jsx'; --- <ReactComponent client:load />
Use client:load to load React components on the client side after the page loads.
You can also use client:idle, client:visible, or client:media for different loading strategies.
MyButton React component when the page loads.import MyButton from './MyButton.jsx'; --- <MyButton client:load />
Counter React component only when it becomes visible on the screen.import Counter from './Counter.jsx'; --- <Counter client:visible />
Widget React component when the browser is idle, improving performance.import Widget from './Widget.jsx'; --- <Widget client:idle />
This example shows a simple React Counter component used inside an Astro page. The button updates the count when clicked. The client:load directive tells Astro to load the React component on the client side.
// File: Counter.jsx import React, { useState } from 'react'; export default function Counter() { const [count, setCount] = useState(0); return ( <button onClick={() => setCount(count + 1)} aria-label="Increment count"> Count: {count} </button> ); } // File: src/pages/index.astro --- import Counter from '../Counter.jsx'; --- <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Astro React Example</title> </head> <body> <h1>Welcome to Astro with React</h1> <Counter client:load /> </body> </html>
Always add client: directives to React components in Astro to control when they load.
Use semantic HTML and ARIA labels inside React components for accessibility.
Astro renders static parts on the server and React handles dynamic parts on the client.
React components add interactivity inside Astro pages.
Use client:load or other client: directives to load React components properly.
This approach combines fast static content with dynamic React features.
Practice
Solution
Step 1: Understand Astro's rendering model
Astro renders static HTML by default for fast loading.Step 2: Role of React components in Astro
React components add interactivity on top of static content.Final Answer:
To add interactive features to a mostly static Astro page -> Option AQuick Check:
React components = interactivity [OK]
- Thinking React replaces Astro's static rendering
- Believing React components run only on server
- Assuming no JavaScript is used with React
Solution
Step 1: Recall Astro client directives
Astro usesclient:loadto load components on the client after page load.Step 2: Identify correct directive for React interactivity
client:loadis the standard for React components to run in browser.Final Answer:
<ReactComponent client:load /> -> Option CQuick Check:
client:load = client-side React load [OK]
- Using client:server which runs only on server
- Using client:static which disables interactivity
- Confusing client:only with client:load
--- import Counter from '../components/Counter.jsx'; ---
Assuming
Counter is a React component that shows a button and a count starting at 0, which increments on click.Solution
Step 1: Understand client:load behavior
Theclient:loaddirective loads the React component in the browser, enabling interactivity.Step 2: React Counter component behavior
The Counter component shows a button with count 0 and increments count on click.Final Answer:
A static button with count 0 that increments on click -> Option AQuick Check:
client:load enables React interactivity [OK]
- Thinking the button is static without interactivity
- Assuming React component won't render without server code
- Believing client:load causes errors
--- import MyButton from '../components/MyButton.jsx'; ---
Assuming
MyButton is a React component that should be interactive on the client.Solution
Step 1: Understand client:server directive
client:servermeans the component runs only on the server, no client interactivity.Step 2: Requirement for React interactivity
To make React interactive in browser, useclient:loador similar client directives.Final Answer:
client:server does not load React components on the client for interactivity -> Option DQuick Check:
client:server = no client interactivity [OK]
- Confusing client:server with client:load
- Assuming React components can't be interactive in Astro
- Thinking import path causes this error
Solution
Step 1: Understand Astro static + React dynamic model
Astro renders static HTML first, React components hydrate on client for interactivity.Step 2: Correct way to hydrate React component
Usingclient:loadloads React on client, enabling data fetching and updates.Final Answer:
Import the React component and use <code>client:load</code> to hydrate it on the client -> Option BQuick Check:
client:load hydrates React for dynamic updates [OK]
- Trying to fetch data only on server without client hydration
- Embedding React code without proper client directives
- Assuming React runs without importing or hydration
Ready to prove it?
Pass the test and this pattern moves unknown
