Creating Your First React App: A Step-by-Step Guide

Welcome! In this tutorial, we will walk you through the process of creating your first React app. React is a JavaScript library that is used for building user interfaces, and it is a popular choice for building web applications because of its declarative approach and reusable components.

Before we get started, make sure that you have Node.js and npm (the package manager for Node) installed on your machine. You can check if you have these dependencies by running the following commands in your terminal:

  • Bm9kZSAtdgpucG0gLXY=

If either of these commands returns a version number, you are good to go. If not, you can download and install Node.js from the official website: https://nodejs.org/ (opens in a new tab).

Now, let's create a new directory for our project and navigate to it:

  • BWtkaXIgbXktcmVhY3QtYXBwCmNkIG15LXJlYWN0LWFwcA==

Next, we will use npm to create a package.json file, which will store information about our project, such as the dependencies we will use. Run the following command to create the package.json file:

  • BnBtIGluaXQgLXk=

The -y flag tells npm to use the default values for all of the prompts, so you don't have to manually enter them.

Now that we have our package.json file set up, we can install React and other dependencies that we will need for our app. Run the following command to install React and React DOM, which is a library that provides DOM-specific methods:

  • BnBtIGluc3RhbGwgcmVhY3QgcmVhY3QtZG9t

Next, we will create a new file called index.html in the root of our project directory. This will be the entry point for our app, and it will contain the basic HTML structure for our app. Add the following code to your index.html file:

PCFET0NUWVBFIGh0bWw+CjxodG1sPgogIDxoZWFkPgogICAgPG1ldGEgY2hhcnNldD0idXRmLTgiIC8+CiAgICA8dGl0bGU+TXkgUmVhY3QgQXBwPC90aXRsZT4KICA8L2hlYWQ+CiAgPGJvZHk+CiAgICA8ZGl2IGlkPSJyb290Ij48L2Rpdj4KICA8L2JvZHk+CjwvaHRtbD4=

The div element with an id of root will be where our React app will be rendered.

Next, we will create a new file called index.js in the root of our project directory. This will be the entry point for our React app, and it will contain the code that initializes our app and renders it to the DOM. Add the following code to your index.js file:

AW1wb3J0IFJlYWN0IGZyb20gJ3JlYWN0JzsKaW1wb3J0IFJlYWN0RE9NIGZyb20gJ3JlYWN0LWRvbSc7Cgpjb25zdCBBcHAgPSAoKSA9PiB7CiAgcmV0dXJuIDxkaXY+SGVsbG8sIFdvcmxkITwvZGl2PjsKfTsKClJlYWN0RE9NLnJlbmRlcig8QXBwIC8+LCBkb2N1bWVudC5nZXRFbGVtZW50QnlJZCgncm9vdCcpKTs=

In this code, we are importing the react and react-dom packages that we installed earlier, and we are creating a simple functional component called App that returns a div element with the text "Hello, World!". We are then using the ReactDOM.render() method to render our App component to the div element with an id of root.

Now that we have our basic React app set up, we need to set up a development server so that we can run and test our app. For this, we will use a tool called Webpack, which is a module bundler that helps us bundle our JavaScript and other assets for the browser.

First, let's install the dependencies we need for Webpack:

  • BnBtIGluc3RhbGwgLS1zYXZlLWRldiB3ZWJwYWNrIHdlYnBhY2stY2xpIHdlYnBhY2stZGV2LXNlcnZlcg==

Next, we will create a configuration file for Webpack called webpack.config.js in the root of our project directory. This file will specify the entry and output points for our app, as well as any loaders and plugins that we want to use.

Add the following code to your webpack.config.js file:

Y29uc3QgcGF0aCA9IHJlcXVpcmUoJ3BhdGgnKTsKCm1vZHVsZS5leHBvcnRzID0gewogIGVudHJ5OiAnLi9pbmRleC5qcycsCiAgb3V0cHV0OiB7CiAgICBwYXRoOiBwYXRoLnJlc29sdmUoX19kaXJuYW1lLCAnZGlzdCcpLAogICAgZmlsZW5hbWU6ICdidW5kbGUuanMnCiAgfSwKICBkZXZTZXJ2ZXI6IHsKICAgIGNvbnRlbnRCYXNlOiAnLi8nLAogICAgcG9ydDogMzAwMAogIH0KfTs=

In this configuration, we are specifying that our entry point is index.js, and the output will be a file called bundle.js in a dist directory. We are also setting up a development server with a content base of the root directory and a port of 3000.

Now that we have our Webpack configuration set up, we can add a script to our package.json file to start the development server. Open up your package.json file and add the following script:

  • InNjcmlwdHMiOiB7CiAgInN0YXJ0IjogIndlYnBhY2stZGV2LXNlcnZlciIKfQ==

With this script in place, we can now start the development server by running the following command:

  • BnBtIHN0YXJ0

This will start the development server and open a new browser window with your app running at http://localhost:3000. You should see the text "Hello, World!" displayed on the page.

That's it! You have now successfully created your first React app. Of course, this is just the beginning, and there is much more to learn about React and how to build web applications with it. But hopefully this tutorial has provided a good foundation for you to start building your own apps.

Your Content Goes Here

More from the blog

Want us to build yours instead?

On a project, every milestone ends with a link to the working build, and the code, the accounts and every password are yours at the handover.

What we’d build for you is on Services.

Send us the brief, or start with the free call.

Page updated .

Privacy preferences

Choose whether this browser sends visit data to our analytics tools. The site works the same either way.

Google Analytics, Amplitude and Microsoft Clarity show us which pages get read. Off means none of the three loads.

Always on

Sentry error reports, with personal data removed, and the Cloudflare spam check on the brief form.

Each cookie is listed in our Cookie Policy.