Astro supports mixing multiple frontend frameworks in the same project, which is one of its most powerful features. You can use React, Vue, Svelte, Preact, SolidJS, and other frameworks simultaneously in a single Astro project.
Integration Steps:
-
Install Framework Integration Packages:
bash# Install React integration npx astro add react # Install Vue integration npx astro add vue # Install Svelte integration npx astro add svelte -
Configure astro.config.mjs:
javascriptimport { defineConfig } from 'astro/config'; import react from '@astrojs/react'; import vue from '@astrojs/vue'; import svelte from '@astrojs/svelte'; export default defineConfig({ integrations: [react(), vue(), svelte()], });
Using Components from Different Frameworks:
astro--- import ReactComponent from '../components/ReactComponent.jsx'; import VueComponent from '../components/VueComponent.vue'; import SvelteComponent from '../components/SvelteComponent.svelte'; import AstroComponent from '../components/AstroComponent.astro'; --- <div> <AstroComponent /> <ReactComponent /> <VueComponent /> <SvelteComponent /> </div>
Framework-Specific Configuration:
React Configuration:
javascriptimport { defineConfig } from 'astro/config'; import react from '@astrojs/react'; export default defineConfig({ integrations: [ react({ // React-specific configuration experimentalDirectRender: false, }), ], });
Vue Configuration:
javascriptimport { defineConfig } from 'astro/config'; import vue from '@astrojs/vue'; export default defineConfig({ integrations: [ vue({ // Vue-specific configuration template: { compilerOptions: { isCustomElement: (tag) => tag.includes('-'), }, }, }), ], });
Svelte Configuration:
javascriptimport { defineConfig } from 'astro/config'; import svelte from '@astrojs/svelte'; export default defineConfig({ integrations: [ svelte({ // Svelte-specific configuration preprocess: [], }), ], });
Best Practices for Mixing Frameworks:
-
Choose Frameworks Based on Needs:
- React: Suitable for complex interactive UIs
- Vue: Suitable for progressive enhancement and rapid development
- Svelte: Suitable for high-performance components
- Astro: Suitable for static content and layouts
-
Maintain Consistency:
- Use the same framework within the same functional module
- Avoid mixing multiple frameworks in a single component
- Establish clear component naming conventions
-
Performance Optimization:
- Only use frameworks for components that need interactivity
- Use
client:*directives to control hydration timing - Consider using lightweight frameworks (like Preact) instead of React
Example: Blog Page
astro--- import Layout from '../layouts/Layout.astro'; import Header from '../components/Header.astro'; import PostList from '../components/PostList.astro'; import CommentSection from '../components/CommentSection.jsx'; // React import LikeButton from '../components/LikeButton.vue'; // Vue import ShareWidget from '../components/ShareWidget.svelte'; // Svelte --- <Layout title="Blog Post"> <Header /> <main> <PostList /> <CommentSection client:visible /> <LikeButton client:idle /> <ShareWidget client:idle /> </main> </Layout>
Data Sharing Between Frameworks:
While components from different frameworks cannot directly share state, you can share data through:
-
Passing Through Props:
astro--- const data = await fetch('/api/data').then(r => r.json()); --- <ReactComponent data={data} /> <VueComponent :data={data} /> -
Using Global State Management:
- Fetch data through APIs
- Use localStorage or sessionStorage
- Use custom event systems
Important Notes:
- Each framework's components need to use the corresponding file extension (
.jsx,.vue,.svelte) - Components from different frameworks cannot directly reference each other
- Ensure all framework dependencies are properly installed
- Some frameworks may require additional configuration (like Vue plugins, React Context)
Astro's multi-framework support allows you to choose the most suitable tools for your project needs while maintaining high performance and excellent developer experience.