React

Setting Up A React Environment

React Course

Introduction

You know what React is and you might even have an idea of where to use it. Now, it’s time to start figuring out how.

This lesson is going to cover starting a new React project on your machine, as well as some useful tools to help you along the way. We’ll also explain some of the problems that may arise (and how to avoid them).

Lesson overview

This section contains a general overview of topics that you will learn in this lesson.

  • How React projects can be created.
  • How to use Vite to create new React projects.
  • How to format the code in React projects.
  • What React Developer Tools are.

Many paths

There are multiple ways to start using React in your projects, from attaching a set of <script> tags which serve React from a CDN, to robust toolchains and frameworks that are highly configurable and allow for increased scalability and optimization.

Some examples of these toolchains include:

Why do we need these toolchains? Can’t we just make our own as we see fit?

Yes, but it’s hard. React is a complex beast and there are many moving parts. Before you can start writing any sort of code that provides functionality, you would need to configure at least the following:

All of this, and sometimes much more is required to get a React project and development environment up and running.

A note on Create React App

Create React App, or CRA, was the official way to scaffold new React projects since its introduction in 2016. Unfortunately, owing to many reasons, CRA was deprecated in early 2023. Read this extensive comment by one of React’s maintainers if you’re curious. Due to CRA’s popularity, you’ll see it mentioned in many tutorials and guides. However, it’s no longer recommended to use for new projects.

Simplifying the process

Now that you understand what is involved with starting a React project from scratch, you can breathe a sigh of relief to learn that we can get started with a single terminal command.

Vite builds frontend tools for developers and it leverages the latest technologies under the hood to provide a great developer experience. Fortunately, it also caters to the React ecosystem. We will use Vite’s CLI to quickly create a template React project. It requires minimal configuration and provides extremely useful tools right out of the box, allowing us to get straight to the learning. Let’s get started!

Creating a React app

Please make sure that you are using the LTS version of Node, otherwise errors may occur. Fire up a terminal instance, cd into the folder containing your projects, and enter the following command:

npm create vite@latest my-first-react-app -- --template react

If you see the following output, enter y and then press enter:

Need to install the following packages:
  create-vite@5.X.X
Ok to proceed? (y)

Once the command has executed, it should output the next steps for you to follow:

cd my-first-react-app
npm install
npm run dev

Provided everything has gone according to plan, head over to localhost:5173, where you’ll be greeted with the following page:

Vite React template homepage

Congratulations! You’ve created your first React app.

As you might’ve noticed by now, you can replace my-first-react-app with the name of your project.

Delving deeper

Let’s take a closer look at our new project. Inside you will find some folders, as well as package.json, package-lock.json, .gitignore, and README.md files. The README.md contains some useful information that you should take the time to skim through now.

The public folder is where all of the static assets related to your app will go. This could include images, icons, and information files for the browser.

Inside the src folder is where you will find the code that runs your app. The main.jsx file here serves as the entry point of the application. Let’s open the main.jsx file and see if we can understand what’s going on:

import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import './index.css'

ReactDOM.createRoot(document.getElementById('root')).render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
)

Whoa! There’s quite a lot here. You are not expected to recognize much of this (if any) right now. Here’s a brief rundown of what is happening:

  1. We import React itself and its fellow ReactDOM package.
  2. We import the App component from App.jsx, so that we may place (render) it within the DOM.
  3. We import some CSS styling (you may recognize this syntax from the Webpack material).
  4. We create a root object by invoking ReactDOM.createRoot with an element from our index.html.
  5. We invoke the render method which is attached to our root object, with some very interesting-looking syntax inside the parentheses.

All of this may look different from anything you’ve seen, but have no fear! Once you’ve finished this course, you’ll know exactly what all of this does, and much more.

Keeping it clean

The starter project ships with ESLint. You could also set up Prettier to help keep your React code as clean as can be.

Developer tools

As you progress with React, your projects will undoubtedly become larger and larger and include more and more components, with increasing levels of functionality.

When this happens, it’s useful to be able to track (and make live changes to) the moving parts inside of your app for understanding and debugging your code. To this end, we can use a Chrome extension called React Developer Tools.

We recommend installing this and becoming comfortable using it as early as possible as it’s an invaluable tool for effective React development.

Assignment

  1. Review this material by reading through Vite’s Getting Started Page.
  2. Check out this guide for React Developer Tools to begin learning how to use it (don’t worry if you don’t understand some of the details yet).
  3. Try to clean up your my-first-react-app project so that it no longer displays the default page. See if you can get it to display a “Hello, World!” message instead.

Knowledge check

This section contains questions for you to check your understanding of this lesson on your own. If you’re having trouble answering a question, click it and review the material it links to.

Additional resources

This section contains helpful links to related content. It isn’t required, so consider it supplemental.

  • It looks like this lesson doesn’t have any additional resources yet. Help us expand this section by contributing to our curriculum.

Support us!

The Odin Project is funded by the community. Join us in empowering learners around the globe by supporting The Odin Project!