Setting Up a React Project: A Beginner's Guide

What is React?

React (or React.js) is a JavaScript library for building user interfaces, developed and maintained by Meta (Facebook). It’s component-based, fast, and widely used for single-page applications (SPAs).

Prerequisites Before You Begin

To set up a React project, make sure you have the following installed:

Step-by-Step: Creating Your First React Project

We will use the official React scaffolding tool called Create React App (CRA).

Step 1: Install Create React App

Open your terminal (Command Prompt or PowerShell) and run:

npx create-react-app my-first-react-app

npx comes with npm 5.2+ and higher – it downloads and runs the latest Create React App without installing it globally.

Β This command creates a folder named my-first-react-app with all boilerplate files.

Step 2: Navigate to the Project Folder

cd my-first-react-app

Step 3: Start the Development Server

npm start

This launches the React development server and opens http://localhost:3000 in your browser.

You’ll see a spinning React logo and a welcome page – Congratulations! You’ve created your first React app!

Folder Structure Overview

When you open the created React app, the folder structure looks like this:

my-first-react-app/
β”œβ”€β”€ node_modules/
β”œβ”€β”€ public/
β”‚   └── index.html
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ App.js        <-- Main component
β”‚   β”œβ”€β”€ index.js      <-- Entry point
β”œβ”€β”€ package.json      <-- Project dependencies & scripts

Key Folders and Files:

public/Β  Contains static assets.

index.html: The single HTML file that React renders into.

favicon.ico: The app’s icon.

src/

Contains the React application code.

App.js: Main React component.

index.js: Entry point for rendering the React application into the DOM.

node_modules/

Stores all dependencies installed via npm.

package.json

Lists the project dependencies and scripts.

Example: Edit the App Component

import React from 'react';

function App() {
  return (
    <div>
      <h1>Hello, React World!</h1>
      <p>This is your first React component.</p>
    </div>
  );
}

export default App;

Now save and your browser will update automatically (thanks to hot reloading).

Useful Commands

CommandDescription
npm startStarts the dev server
npm run buildPrepares production build
npm testRuns unit tests
npm run ejectExposes config (advanced use only)

Deploying Your React App (Optional)

To deploy a production version, run:

npm run build

Then you can deploy the build/ folder using:

  • Netlify

  • Vercel

  • GitHub Pages

Common Issues and Fixes

ProblemSolution
npx not recognizedReinstall Node.js or update npm
Port 3000 already in useUse another port: PORT=3001 npm start (Unix)
Syntax error in JSXMake sure to close all tags and use className