Master React From Scratch

Clear, practical, and structured React lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to React
Introduction to React What Is React? Features of React Advantages of React Limitations of React Uses of React React vs JavaScript React Architecture
2. React Getting Started
Getting Started Introduction Prerequisites Installing Node.js and npm Creating a React Project with Vite React Project Structure Running React Application Your First React Application
3. JSX
JSX Introduction What Is JSX? JavaScript Expressions JSX Attributes Rendering JSX Elements JSX Children React Fragments JSX Rules and Best Practices JSX vs HTML
4. React Components
Components Introduction Functional Components Class Components Component Composition Reusable Components Component Tree Component Lifecycle Import and Export Components
5. React Props
Props Introduction What Are Props? Passing Props to Components Props Destructuring Default Props Values Children Props Typing Props with TypeScript Props vs State
6. React State
State Introduction What Is State? useState Hook Updating State Functional State Updates State with Objects State with Arrays State Immutability Lifting State Up
7. React Events
Events Introduction Handling Events Passing Arguments to Event Handlers Synthetic Events Form Events Keyboard and Mouse Events Event Propagation
8. Conditional Rendering
Conditional Rendering Introduction If-Else Rendering Ternary Operator Logical AND Operator Element Variables Preventing Rendering Multiple Conditions Conditional CSS Classes
9. Lists and Keys
Lists and Keys Introduction Rendering Lists with map() Understanding Keys Choosing Unique Keys Lists of Objects Filtering Lists Sorting Lists Empty Lists and Conditional Rendering
10. React Forms
Forms Introduction Handling Forms Controlled Components Uncontrolled Components Handling Input Fields Checkboxes and Radio Buttons Select and Textarea Form Validation Form Submission
11. React Hooks
Hooks Introduction useState Hook useEffect Hook Effect Dependencies and Cleanup useRef Hook useReducer Hook useMemo Hook useCallback Hook Custom Hooks Rules of Hooks
12. React Context API
Context API Introduction createContext useContext Context Provider Reading Context Updating Context Using Multiple Contexts Context API vs Props Context API Best Practices
13. React Routing
Routing Introduction Introduction to React Router Installing React Router BrowserRouter Setup Routes and Route Components Link and NavLink Programmatic Navigation URL Parameters and useParams Nested Routes Not Found Routes Protected Routes
14. API Integration
API Integration Introduction Fetch API Async/Await with API Calls Displaying API Data Loading and Error States Sending POST Requests API Error Handling Using Axios Custom Fetch Hook API Environment Variables
15. State Management
State Management Introduction Global State Prop Drilling Introduction to Redux Redux Core Concepts Redux Toolkit Store and Slices useSelector and useDispatch Async Operations with Redux Toolkit State Management Best Practices
16. React Performance
Performance Introduction Understanding Re-Renders Avoiding Unnecessary Re-Renders React.memo useMemo Optimization useCallback Optimization React Developer Tools and Profiler Lazy Loading Components Code Splitting Optimizing Large Lists Performance Best Practices

Creating a React Project with Vite

Learn how to create a React application using Vite, install its dependencies, understand the basic commands, and start the project.

1. What Is Vite?

Vite is a frontend development tool used to create and run modern web applications. It provides a development server and tools for building applications for production.

We can use Vite to create a React project without manually configuring every development tool.

Remember: React is the library used to build the user interface. Vite helps create, run, and build the React project.

2. React Project Creation Flow

Follow these steps to create and run your first Vite-powered React application.

Step 1

Open Terminal

Use Command Prompt, PowerShell, or VS Code terminal.

↓
Step 2

Create Vite Project

Generate a React project from the command line.

↓
Step 3

Install Dependencies

Install the packages specified by the project.

↓
Step 4

Run Development Server

Start the application using the npm script.

↓
✓

React Application Ready

Open the local URL in your browser.

3. Before Creating the Project

Make sure the following items are ready:

✓

Node.js

Installed on your computer with a version supported by the current Vite release.

✓

npm

Available in your terminal for installing project packages.

✓

Terminal

Open Command Prompt, PowerShell, or the VS Code terminal.

✓

Code Editor

Visual Studio Code or another editor for editing project files.

Check first: Run node --version and npm --version before continuing. If the current Vite release requires a newer Node.js version, update Node.js before creating the project.

4. Step 1: Open the Terminal

First, decide where you want to save your React project. Open a terminal and navigate to that location.

Example: Navigate to the Desktop

Terminal Windows
cd Desktop

This command moves into the Desktop folder when it is available relative to your current terminal location.

Tip: You can also open a folder in VS Code and use Terminal → New Terminal. The terminal usually starts in the workspace folder.

5. Step 2: Create a React Project Using Vite

Run the following command to create a project named my-react-app using Vite's React template.

Terminal Project Creation
npm create vite@latest my-react-app -- --template react

Understand the Command

npm create vite@latest

Runs the Vite project creation tool.

my-react-app

Sets the name of your new project folder.

-- --template react

Passes the React template option to the Vite scaffolding tool.

Important: If npm displays a confirmation or installation prompt, read the prompt and follow the instructions in your terminal.

6. Step 3: Enter the Project Folder

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

Terminal Change Directory
cd my-react-app

This command makes the project folder the current working directory. The next commands should be run from inside it.

Current Project Folder
my-react-app\

7. Step 4: Install Project Dependencies

A newly generated project contains a package.json file. This file describes the dependencies and available npm scripts for the application.

Run the following command inside the project folder:

Terminal Install Packages
npm install

What Does npm install Do?

Reads package.json

Checks the project's dependency definitions.

Downloads Packages

Retrieves the dependencies required by the project.

Creates node_modules

Stores installed project packages in the dependency directory.

Creates or Updates Lockfile

Uses npm's lockfile to record resolved dependency versions.

Note: Installation time depends on your internet connection and the packages needed by the project.

8. Step 5: Run the React Application

After installing the dependencies, use the project's development script to start the development server.

Terminal Development Server
npm run dev

Vite will display a local URL in the terminal. The default address commonly uses port 5173, but the actual URL may differ if another port is needed.

Local development server
Example URL
http://localhost:5173/

Open the exact URL printed in your terminal.

Remember: Keep the terminal running while you work. To stop the development server, focus the terminal and press Ctrl + C.

9. All Commands Together

Here is the complete sequence for creating and running the React project from the terminal.

Terminal Complete Setup
npm create vite@latest my-react-app -- --template react
cd my-react-app
npm install
npm run dev
1Create the React project.
2Enter the project directory.
3Install its dependencies.
4Run the development server.

10. Preview of the Project Structure

Vite generates a starting set of files for the selected template. A React project commonly contains a structure similar to this:

📁 my-react-app
📁 public
📁 src
📄 App.jsx
📄 App.css
📄 index.css
📄 main.jsx
📄 index.html
📄 package.json
📄 package-lock.json
📁 node_modules

src/App.jsx

Contains the main application component.

src/main.jsx

Connects the React application to the HTML root element.

package.json

Contains project metadata, dependencies, and npm scripts.

node_modules

Contains the packages installed for the project.

The exact files may vary depending on the Vite version and template used. We will explore the project structure in the next lesson.

11. Try a Simple React Component

After the application is running, open src/App.jsx in your editor. Replace its content with the following small example.

src/App.jsx React JSX
function App() {
    return (
        <div>
            <h1>Welcome to React!</h1>
            <p>My first React project is running.</p>
        </div>
    );
}

export default App;

Expected Browser Output

Example Output

Welcome to React!

My first React project is running.

Important: This is a static illustration of the expected output. The actual output appears in your own browser after you save the file and the application compiles successfully.

12. Common Errors and Solutions

Problem Possible Reason Solution
Node.js version error Installed Node.js does not meet the Vite requirements. Install a supported Node.js version and reopen the terminal.
Project creation fails Network, npm, or command-line issue. Check the exact terminal error and try again.
npm install fails Network or package dependency issue. Check internet access and the error details.
Missing script: dev The current folder does not have the expected project script. Check the current folder and the scripts in package.json.
Browser page does not load The server may not be running or the URL may be incorrect. Check the terminal and open the displayed local URL.
Port already in use Another process uses the default port. Use the alternative URL Vite displays in the terminal.

Debugging Tip

Read the terminal message first. It often identifies whether the problem is related to Node.js, dependencies, project configuration, or the development server.

13. Practice Task

Task: Create Your First Vite React Project

  1. Open a new terminal.
  2. Create a project named my-react-app.
  3. Move into the project folder.
  4. Install the dependencies with npm install.
  5. Start the app using npm run dev.
  6. Open the local URL provided by Vite.
  7. Replace the content of src/App.jsx with the sample above.
  8. Save the file and verify the output in your browser.

Summary

  • Vite helps create and run a React project.
  • npm create vite@latest starts project creation.
  • cd my-react-app enters the project folder.
  • npm install installs project dependencies.
  • npm run dev starts the development server.
  • src/App.jsx contains the main component in this example.
  • Use the local URL shown in your terminal to open the application.