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

Setting Up BrowserRouter

In this lesson, we will learn how to configure BrowserRouter, wrap our React application with it, and prepare the application for URL-based navigation.

1. What Is BrowserRouter?

BrowserRouter is a component provided by React Router that uses the browser's URL and history to manage client-side navigation in a React application.

It provides routing context to the components rendered inside it, allowing them to use routing features such as Routes, Route, Link, and navigation hooks.

Remember: BrowserRouter provides the routing environment. We define individual URL paths using Route components inside Routes.

2. Why Do We Need BrowserRouter?

  • To enable browser-based routing.
  • To keep track of the current URL.
  • To support client-side navigation between routes.
  • To provide routing context to child components.
  • To work with browser history during navigation.

3. Basic BrowserRouter Structure

BrowserRouter generally wraps the part of the application that needs routing. Routes and Route are placed inside it to define the pages that users can access.

BrowserRouter
↓
Routes
↓
Route Definitions
↓
Matching Page Component

4. Import BrowserRouter

First, import BrowserRouter from react-router-dom.

import { BrowserRouter } from 'react-router-dom';

If the package is not installed, run the following command from the React project directory.

npm install react-router-dom

5. Set Up BrowserRouter in main.jsx

In a Vite React project, main.jsx is commonly the entry point of the application. You can wrap the App component with BrowserRouter here.

import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';
import App from './App.jsx';

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

Explanation

  • BrowserRouter: Enables browser-based routing.
  • App: Contains the main application components.
  • ReactDOM.createRoot: Mounts the React application.
  • React.StrictMode: Helps identify potential problems during development.

Important: In this setup, do not wrap App in another BrowserRouter inside App.jsx. Use one BrowserRouter for this routing tree.

6. Define Routes in App.jsx

After setting up BrowserRouter, define the application's routes using Routes and Route.

import { Routes, Route } from 'react-router-dom';

function Home() {
    return <h2>Home Page</h2>;
}

function About() {
    return <h2>About Page</h2>;
}

function App() {
    return (
        <div>
            <h1>CIIT Training Institute</h1>

            <Routes>
                <Route path="/" element={<Home />} />
                <Route path="/about" element={<About />} />
            </Routes>
        </div>
    );
}

export default App;

Expected Output

When you open the root URL:

CIIT Training Institute
Home Page

When you open the /about URL:

CIIT Training Institute
About Page

These outputs assume the development server is running and the corresponding URL is opened in the browser.

7. BrowserRouter and Link

Once BrowserRouter is configured, components inside it can use Link to navigate between defined routes.

import { Link } from 'react-router-dom';

function Navigation() {
    return (
        <nav>
            <Link to="/">Home</Link>
            {' | '}
            <Link to="/about">About</Link>
        </nav>
    );
}

export default Navigation;

Render Navigation inside the application where you want the links to appear. Both destination paths must have matching route definitions.

8. Common BrowserRouter Errors

Error 1: Link used outside a router

Components that use Link or routing hooks must be rendered inside the appropriate router provider.

Error 2: Multiple BrowserRouter wrappers

Avoid unnecessarily nesting BrowserRouter instances around the same routing tree.

Error 3: Route does not match

Verify that the browser URL matches the path configured in the Route component.

Error 4: Direct URL returns a server error

In a deployed single-page application, the web server may need to serve the application entry point for supported client-side routes. Configure the appropriate fallback for your hosting environment.

Practice Tasks

  1. Explain the purpose of BrowserRouter.
  2. Install React Router in a React project.
  3. Configure BrowserRouter in main.jsx.
  4. Create Home and About components.
  5. Define routes for / and /about.
  6. Create navigation links using Link.
  7. Test both routes in the browser.
  8. Identify and fix a routing error caused by a missing router provider.

Summary

BrowserRouter provides browser-based routing for a React application and makes routing context available to its child components. It can wrap the App component in main.jsx, while Routes and Route define the URL paths and their corresponding components. Link can then navigate between those routes. In the next lesson, we will study Routes and Route components in detail.