Back

Introduction to Astro with React: Your first project

July 2, 2024

5 min read

Introduction to Astro with React: A Comprehensive Guide for Your First Project

Astro is a modern web development framework designed to create fast and optimized websites. Its ability to integrate multiple JavaScript frameworks, such as React, Vue, and Svelte, makes it especially appealing to developers seeking flexibility and performance. In this article, we will guide you step-by-step through the creation of your first React project with Astro, ideal for those looking to get started with this powerful combination.

What is Astro?

Astro is a static site generator that focuses on speed and simplicity. By using Astro, we can benefit from delivering optimized static HTML content and adding interactivity only when necessary by using JavaScript components. This results in faster load times and significantly improved performance compared to traditional approaches that load large amounts of JavaScript upfront.

Key Features of Astro

Installing Astro

To get started with Astro, we need to have Node.js and npm (Node Package Manager) installed on our machine. Here are the initial steps to create a new Astro project:

1. Create a new project:

npm create astro@latest

   During this step, we will be prompted to choose a template for our project. We can select a basic template to start.

2. Navigate to the project folder:

cd project-name

   Replace project-name with the name you gave your project.

3. Install dependencies:

npm install

Configuring the Project with React

To use React in our Astro project, we need to install the React integration package. This package allows us to use React components within our Astro project.

1. Install the React integration:

    npm install @astrojs/react    

2. Update the astro.config.mjs file:

   Edit the astro.config.mjs file to include the React integration. This file contains the main configuration for Astro.

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

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

Creating a Component in React

Now that we have React configured, let’s create a simple React component and use it within an Astro page.

1. Create a Hello.jsx component in the src/components folder:

import React from 'react';

const Hello = () => {
  return <h1>Hello, Astro with React!</h1>;
};

export default Hello;

2. Use the component in an Astro page:

   Create a new index.astro file in the src/pages folder and use the Hello component we just created.


   ---

   import Hello from '../components/Hello.jsx';

   ---

   <html>

     <head>

       <title>My First Astro Project</title>

     </head>

     <body>

       <Hello />

     </body>

   </html>

Structure of an Astro Project

As our project grows, it is important to understand the project structure to keep the code organized and maintainable. Here is an overview of a typical Astro project structure:

Deploying the Project on Netlify

Netlify is a popular hosting platform that simplifies the deployment process for websites. Let’s use Netlify to deploy our Astro project.

1. Install Netlify CLI:


   npm install -g netlify-cli

2. Log in to Netlify:


   netlify login

   This will open a window in our browser to log in to our Netlify account.

3. Build the project:


   npm run build

   This command will generate the static files needed for deployment in the dist folder.

4. Deploy the project:


   netlify deploy

   Follow the on-screen instructions to select the output directory, which is usually dist. Once done, Netlify will provide a temporary URL for our deployed site.

5. Final deploy:


   netlify deploy --prod

   This will deploy our project to production, making it accessible to the public at a permanent URL.

Best Practices in Developing with Astro and React

To make the most out of Astro and React’s capabilities, it is important to follow some best practices:

Conclusion

We have learned how to create a basic Astro project with React, including setting up the environment, creating a component, and deploying the project on Netlify. Astro is a powerful tool for developing fast and modern websites, and its flexibility with multiple frameworks makes it an attractive option for developers of all levels. With these skills, we are ready to explore more advanced features and build more complex projects. Good luck on your journey with Astro and React!


This is the extended version of the article. If you need more details or specific adjustments, feel free to let me know.