You now know what React is and you might even have an idea of where to use it, 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).
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 are React Developer Tools.
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:
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 it for new projects.
Now that you understand what is involved with starting a React project from scratch, you can breathe a sigh of relief as you 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, they also cater 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 straight out of the box, allowing us to get straight to the learning. Let’s get started!
Please make sure that you are using the LTS version of Node, otherwise errors may occur. Fire up a terminal instance,
cd over to 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@4.X.X Ok to proceed? (y)
Once the command had begun executing, it should output the next steps for you to follow:
cd my-first-react-app npm install npm run dev
Provided that everything has gone according to plan, head over to
localhost:5173, where you’ll be greeted with the following page:
Congratulations! You’ve created your first React app.
You might’ve noticed by now; you can replace
my-first-react-app with the name of your project.
Let’s take a closer look at our new project. Inside you will find some folders, as well as
README.md files. The
README.md contains some useful information that you should take the time to skim through now.
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.
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 here:
- We import
Reactitself, and its fellow
- We import the
App.jsx, so that we may place (render) it within the DOM.
- We import some CSS styling (you may recognize this syntax from the Webpack material).
- We create a
rootobject by invoking
ReactDOM.createRootwith an element from our
- We invoke the
rendermethod which is attached to our
rootobject, with some very interesting-looking syntax inside the parentheses.
All of this may understandably look unlike anything you’ve seen up until now, but have no fear, once you’ve spent the time with this course, you’ll know exactly what all of this does, and much more.
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 becomes very 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 make use of a Chrome extension called React Developer Tools.
We recommend installing this and becoming comfortable with using it as early as possible as it is an invaluable tool for effective React development.
- Review this material by reading through Vite’s Getting Started Page.
- Check out this guide for React Developer Tools to begin learning how to utilize it effectively (don’t worry if you can’t yet understand some of the languages).
- Try to clean up your
my-first-react-appproject so that it no longer displays the default page, see if you can get it to display a “Hello, World!” message instead.
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.
- What are some of the ways we can start a new React project?
- Why should we initially be using pre-made toolchains instead of making our own?
- What is Vite and why would we use it?
- What command can we run to scaffold a new React project using Vite?
- What is in the
- What is in the
- Why are the React Developer Tools useful?
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.