Back to Course

Learn React

0% Complete
0/0 Steps
Lesson 2 of 32
In Progress

Setting Up a React Development Environment

In this article, we’ll go through the process of setting up a development environment for React. This includes installing the necessary tools and libraries, creating a new project, and running your first React application.

Prerequisites

Before we get started, there are a few prerequisites that you’ll need to have in place:

  • Node.js – React is built with JavaScript, and you’ll need to have Node.js installed in order to use React. Node.js is a runtime environment that allows you to execute JavaScript on the server-side, and it includes a package manager called npm (Node Package Manager) that you’ll use to install libraries and tools.
  • git – Git is a version control system that allows you to track changes to your code and collaborate with other developers. It is not required to use React, but it is a useful tool to have in your development workflow.

Installing create-react-app

The easiest way to get started with React is to use a tool called create-react-app. create-react-app is a command-line utility that generates a new React project with a development server, build scripts, and a bunch of other useful stuff all set up and ready to go.

To install create-react-app, open a terminal and run the following command:

npm install -g create-react-app

This will install create-react-app globally, which means that you’ll be able to use it to create new React projects from anywhere on your machine.

Creating a new project

Now that create-react-app is installed, you can use it to create a new React project. To do this, navigate to the directory where you want to create your project and run the following command:

create-react-app my-project

Replace “my-project” with the name of your project. create-react-app will create a new directory with the same name as your project and generate all of the files and dependencies that you’ll need to get started. This process may take a few minutes.

Running the development server

Once create-react-app has finished creating your project, you can start the development server by navigating to the project directory and running the following command:

cd my-project
npm start

This will start the development server and open a new browser window with your React application running. The development server includes hot reloading, which means that any changes that you make to your code will be automatically reflected in the browser.

Your first React application

If everything is set up correctly, you should see a page with the text “Welcome to React” displayed in the browser. This is a simple React application that is generated by create-react-app when you create a new project.

The main entry point for your React application is the src/index.js file. This file is responsible for rendering your React application to the DOM. If you open this file, you’ll see the following code:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(
  <React.StrictMode>
<App />
  </React.StrictMode>,
  document.getElementById('root')
);
// If you want your app to work offline 
// and load faster, you can change
// unregister() to register() below. 
// Note this comes with some pitfalls.
// Learn more about service workers: 
// https://bit.ly/CRA-PWA
serviceWorker.unregister();

The code above imports the necessary dependencies, including the `App` component, which is the root component of your React application. It then uses the `ReactDOM.render()` function to render the `App` component to the DOM. The `App` component is located in the `src/App.js` file, and it is a good place to start building your React application. The `App` component is just a regular React component that is created using a JavaScript class. If you open the `src/App.js` file, you’ll see the following code:

import React from 'react';
import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;

The App component is a function that returns a JSX element. JSX is a syntax extension for JavaScript that allows you to write HTML-like code in your React components. It makes it easier to write and read React components, and it is a key part of the React ecosystem.

You can modify the App component to customize the appearance and behavior of your React application. For example, you could add a new component or change the text that is displayed.

Conclusion

In this article, we’ve gone through the process of setting up a development environment for React. We’ve installed the necessary tools and libraries, created a new project with create-react-app, and run our first React application. Now that you have a development environment set up, you’re ready to start building your own React applications.

Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

What is create-react-app?

create-react-app is a command-line utility that generates a new React project with a development server, build scripts, and a bunch of other useful stuff all set up and ready to go.

What is the main entry point for a React application?

The main entry point for a React application is the src/index.js file. This file is responsible for rendering the React application to the DOM.

What is the App component in a create-react-app project?

The App component is the root component of a create-react-app project. It is located in the src/App.js file and is a good place to start building your React application.

What is JSX?

JSX is a syntax extension for JavaScript that allows you to write HTML-like code in your React components. It makes it easier to write and read React components and is a key part of the React ecosystem.

How do you start the development server in a create-react-app project?

To start the development server in a create-react-app project, navigate to the project directory and run the npm start command. This will start the development server and open a new browser window with your React application running. The development server includes hot reloading, which means that any changes that you make to your code will be automatically reflected in the browser.