Migrating from Create React App (CRA)
Astro’s React integration provides support for using React components inside Astro components, including entire React apps like Create React App (CRA)!
---// Import your root App componentimport App from '../cra-project/App.jsx';---<!-- Use a client directive to load your app --><App client:load />
Many apps will “just work” as full React apps when you add them directly to your Astro project with the React integration installed. This is a great way to get your project up and running immediately and keep your app functional while you migrate to Astro.
Over time, you can convert your structure piece-by-piece to a combination of .astro
and .jsx
components. You will probably discover you need fewer React components than you think!
Here are some key concepts and migration strategies to help you get started. Use the rest of our docs and our Discord community to keep going!
Key Similarities between CRA and Astro
Section titled Key Similarities between CRA and Astro-
The syntax of
.astro
files is similar to JSX. Writing Astro should feel familiar. -
Astro uses file-based routing, and allows specially named pages to create dynamic routes.
-
Astro is component-based, and your markup structure will be similar before and after your migration.
-
Astro has official integrations for React, Preact, and Solid so you can use your existing JSX components. Note that in Astro, these files must have a
.jsx
or.tsx
extension. -
Astro has support for installing NPM packages, including React libraries. Many of your existing dependencies will work in Astro.
Key Differences between CRA and Astro
Section titled Key Differences between CRA and AstroWhen you rebuild your CRA site in Astro, you will notice some important differences:
-
CRA is a single-page application that uses
index.js
as your project’s root. Astro is a multi-page site, andindex.astro
is your home page. -
.astro
components are not written as exported functions that return page templating. Instead, you’ll split your code into a “code fence” for your JavaScript and a body exclusively for the HTML you generate. -
content-driven: Astro was designed to showcase your content and to allow you to opt-in to interactivity only as needed. An existing CRA app might be built for high client-side interactivity and may require advanced Astro techniques to include items that are more challenging to replicate using
.astro
components, such as dashboards.
Add your CRA to Astro
Section titled Add your CRA to AstroYour existing app can be rendered directly inside a new Astro project, often with no changes to your app’s code.
Create a new Astro project
Section titled Create a new Astro projectUse the create astro
command for your package manager to launch Astro’s CLI wizard and select a new “empty” Astro project.
npm create astro@latest
pnpm create astro@latest
yarn create astro@latest
Add integrations and dependencies
Section titled Add integrations and dependenciesAdd the React integration using the astro add
command for your package manager. If your app uses other packages supported by the astro add
command, like Tailwind and MDX, you can add them all with one command:
npx astro add reactnpx astro add react tailwind mdx
pnpm astro add reactpnpm astro add react tailwind mdx
yarn astro add reactyarn astro add react tailwind mdx
If your CRA requires any dependencies (e.g. NPM packages), then install them individually using the command line or by adding them to your new Astro project’s package.json
manually and then running an install command. Note that many, but not all, React dependencies will work in Astro.
Add your existing app files
Section titled Add your existing app filesCopy your existing Create React App (CRA) project source files and folders (e.g. components
, hooks
, styles
, etc.) into a new folder inside src/
, keeping its file structure so your app will continue to work. Note that all .js
file extensions must be renamed to .jsx
or .tsx
.
Do not include any configuration files. You will use Astro’s own astro.config.mjs
, package.json
, and tsconfig.json
.
Move the contents of your app’s public/
folder (e.g. static assets) into Astro’s public/
folder.
Directorypublic/
- logo.png
- favicon.ico
- …
Directorysrc/
Directorycra-project/
- App.jsx
- …
Directorypages/
- index.astro
- astro.config.mjs
- package.json
- tsconfig.json
Render your app
Section titled Render your appImport your app’s root component in the frontmatter section of index.astro
, then render the <App />
component in your page template:
---import App from '../cra-project/App.jsx';---<App client:load />
Your app needs a client directive for interactivity. Astro will render your React app as static HTML until you opt-in to client-side JavaScript.
Use client:load
to ensure your app loads immediately from the server, or client:only="react"
to skip rendering on the server and run your app entirely client-side.
Convert your CRA to Astro
Section titled Convert your CRA to AstroAfter adding your existing app to Astro, you will probably want to convert your app itself to Astro!
You will replicate a similar component-based design using Astro HTML templating components for your basic structure while importing and including individual React components (which may themselves be entire apps!) for islands of interactivity.
Every migration will look different and can be done incrementally without disrupting your working app. Convert individual pieces at your own pace so that more and more of your app is powered by Astro components over time.
As you convert your React app, you will decide which React components you will rewrite as Astro components. Your only restriction is that Astro components can import React components, but React components must only import other React components:
---import MyReactComponent from '../components/MyReactComponent.jsx';---<html> <body> <h1>Use React components directly in Astro!</h1> <MyReactComponent /> </body></html>