Lesson 8 of 60 – Creating Your First React App
13%

Creating Your First React App

In this lesson, you will learn how to create your first React application. We will use Node.js, npm, and a modern React project setup to create and run a simple application.

Note: Before starting this lesson, make sure Node.js and npm are installed and that commands such as node --version and npm --version work in your terminal.

1. What is a React App?

A React app is a web application whose user interface is built using React components.

React App
   ↓
Components
   ↓
User Interface
   ↓
Web Browser

2. Requirements for a React App

Before creating a React project, you should have a suitable development environment.

  • Node.js
  • npm
  • VS Code or another code editor
  • A modern web browser
  • Basic JavaScript knowledge

3. Check Node.js

Open your terminal and check whether Node.js is installed.

node --version

The terminal should display the installed Node.js version.

4. Check npm

Check whether npm is available on your system.

npm --version

If npm is installed correctly, its version number will be displayed.

5. Open Your Project Folder

Open a terminal and move to the location where you want to create your React project.

cd Desktop

You can choose another folder according to your requirements.

6. Create a React Project

A common modern approach is to create a React project using a development tool such as Vite.

npm create vite@latest my-react-app

This command starts the project creation process.

7. Choose the Project Framework

During project creation, the tool asks you to choose a framework or library.

Select a framework:
> React
  Vue
  Svelte
  Others

Select React for this tutorial.

8. Choose a Variant

After selecting React, the setup tool asks you to choose a variant. For a beginner React project, you can choose a JavaScript-based option.

React
  ↓
JavaScript
  ↓
Project Created

A TypeScript option can also be selected when you specifically want to use TypeScript.

9. Move into the Project Folder

After the project has been created, move into its directory.

cd my-react-app

Your terminal is now working inside the React project.

10. Install Project Dependencies

Install the packages required by the project.

npm install

npm reads the project's package configuration and installs the required dependencies.

11. Start the Development Server

After installing dependencies, start the development server.

npm run dev

The development tool will display a local address where you can open the application in your browser.

12. Local Development URL

The development server normally provides a local URL. The exact port can vary depending on the development tool and whether the default port is available.

http://localhost:5173/

Open the displayed address in your browser.

13. Open the Project in VS Code

You can open the project folder in VS Code.

cd my-react-app
code .

The code . command works when the VS Code command-line launcher is configured on your system.

14. React Project Structure

A newly created project contains several files and directories.

my-react-app/
│
├── public/
├── src/
├── package.json
├── package-lock.json
└── node_modules/

The exact files can vary depending on the project setup and tool version.

15. The src Folder

The src folder generally contains the application's source code.

src/
 |
 +-- App.jsx
 +-- main.jsx
 +-- assets/

This is where you will spend much of your time while developing the React application.

16. The App Component

The App component commonly represents the main component of a simple React application.

function App() {

    return (
        <h1>Hello React</h1>
    );

}

export default App;

17. The main.jsx File

The application's entry file typically connects the React component tree to an HTML element in the page.

import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import App from './App.jsx';

createRoot(document.getElementById('root')).render(
    <StrictMode>
        <App />
    </StrictMode>
);

The exact generated code can vary with the project setup.

18. The Root Element

React needs a DOM element where the React application can be rendered.

<div id="root"></div>

The React entry file selects this element and renders the component tree into it.

19. Change the App Component

Open App.jsx and replace the existing component content with a simple example.

function App() {

    return (
        <div>
            <h1>My First React App</h1>
            <p>Welcome to React!</p>
        </div>
    );

}

export default App;

20. Save the File

After changing the component, save the file.

App.jsx
   ↓
Save
   ↓
Development Server
   ↓
Browser Update

Modern development servers can update the browser automatically when source files change.

21. Hot Module Replacement

Modern development tools can update parts of the application while you are developing it. This is commonly called Hot Module Replacement (HMR).

Change Code
    ↓
Save File
    ↓
Development Tool
    ↓
Browser Updates

22. Add a Variable

React components can use normal JavaScript variables.

function App() {

    const name = "Soopro Pathshala";

    return (
        <h1>Welcome to {name}</h1>
    );

}

export default App;

The value of name is displayed using JSX expression syntax.

23. Add a Button

You can add HTML-like elements using JSX.

function App() {

    return (
        <div>

            <h1>My React App</h1>

            <button>
                Click Me
            </button>

        </div>
    );

}

24. Add a Click Event

React can respond to user actions using event handlers.

function App() {

    function handleClick() {
        alert("Hello React!");
    }

    return (
        <button onClick={handleClick}>
            Click Me
        </button>
    );

}

25. Complete Simple App

function App() {

    const course = "React";

    function handleClick() {
        alert("Welcome to " + course);
    }

    return (
        <div>

            <h1>My First React App</h1>

            <p>
                I am learning {course}.
            </p>

            <button onClick={handleClick}>
                Click Me
            </button>

        </div>
    );

}

export default App;

26. Run the Application Again

If the development server is not already running, execute:

npm run dev

Then open the local URL shown in the terminal.

Terminal
   ↓
npm run dev
   ↓
Local URL
   ↓
Browser
   ↓
React App

27. Stop the Development Server

When you want to stop a development server running in a terminal, the standard interrupt command for the terminal can be used.

Ctrl + C

This stops the current development process in that terminal.

28. Common Errors

Beginners may encounter common problems while creating a React application.

  • Node.js is not installed.
  • npm is not available in the terminal.
  • The wrong project directory is open.
  • Dependencies have not been installed.
  • A source file contains a syntax error.
  • The development server is not running.
  • The browser is using an old page state.

29. Complete React App Creation Flow

Install Node.js
      ↓
Check Node.js
      ↓
Check npm
      ↓
Create React Project
      ↓
Choose React
      ↓
Choose JavaScript
      ↓
cd my-react-app
      ↓
npm install
      ↓
npm run dev
      ↓
Open Browser
      ↓
Edit App.jsx
      ↓
Build Your UI

30. Summary of Creating Your First React App

Creating a React application involves setting up Node.js and npm, creating a project, installing its dependencies, starting the development server, and editing React components.

Node.js
   ↓
npm
   ↓
React Project
   ↓
Components
   ↓
Development Server
   ↓
Browser
   ↓
React Application

You have now created the basic structure of a React application. In the next lessons, you will learn how the files and folders inside a React project work together.

📌 Key Points

  • Node.js and npm are commonly used for React development tooling.
  • A React project can be created using a modern project setup tool such as Vite.
  • npm create vite@latest my-react-app starts the project creation process.
  • After creating the project, move into the project directory.
  • npm install installs project dependencies.
  • npm run dev starts the development server when defined by the project.
  • The src folder contains application source code.
  • App.jsx can contain the main application component.
  • main.jsx commonly serves as an entry point for rendering the app.
  • JSX can contain JavaScript expressions inside curly braces.
  • React components can respond to user events.
  • Modern development tools can update the browser while code is being edited.

🧠 Quick Quiz

Question: Which command is commonly used to start a Vite-based React development server?