Performance by Default – Astro ships minimal JavaScript to the browser, unlike typical React SPAs that send large bundles.
Granular Hydration – You can choose which React components hydrate on load, on idle, or on visibility.
SEO-Friendly – Astro outputs fully rendered HTML, ensuring search engines can crawl your content efficiently.
Flexibility – Use React for interactivity without turning your entire site into a JavaScript-heavy SPA.
Create a New Astro Project
npm create astro@latest
Add React Support
npm install @astrojs/react
Configure Astro for React
In astro.config.mjs:
import { defineConfig } from 'astro/config';
import react from '@astrojs/react';
export default defineConfig({ integrations: [react()], });
You can import React components directly into .astro files:
--- import Counter from '../components/Counter.jsx'; ---
<html>
<body>
<h1>Welcome to Astro + React</h1>
<Counter client:load />
</body>
</html>
Here, client:load ensures the component hydrates only in the browser, improving initial load speed.
Astro provides five hydration strategies for React components:
client:load – Load immediately after page load.
client:idle – Load after the browser is idle.
client:visible – Load when the component enters the viewport.
client:media="(max-width: 600px)" – Conditional loading based on media queries.
client:only="react" – Load only in the client, no server rendering.
Code Splitting – Import components dynamically:
const Chart = React.lazy(() => import('./Chart'));
Static HTML Output – Use Astro’s build process to prerender non-interactive content.
Astro CDN – Cache assets at the edge for ultra-fast delivery.
Minimal JavaScript – Hydrate only essential React components, leaving the rest static.
Marketing Sites – Static landing pages with React-powered forms or sliders.
Blogs – Static content with interactive comments or like buttons.
E-Commerce – Pre-rendered product listings with dynamic shopping carts.
Pairing Astro with React allows developers to strike the perfect balance between speed, SEO, and interactivity. By leveraging Astro’s partial hydration and CDN optimizations, you can ship blazing-fast, modern web applications without sacrificing the rich UI capabilities that make React so powerful.