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

Introduction to React Router

In this lesson, we will understand React Router, why it is useful, its main components, and how it connects URL paths with React components.

1. What Is React Router?

React Router is a routing library for React applications. It allows developers to display different components based on the current URL and navigate between application views.

For example, a training institute website can display its Home page at /, Courses page at /courses, and Contact page at /contact.

Remember: React Router helps manage navigation in a React application by connecting URL paths to the appropriate components.

2. Why Do We Use React Router?

  • To create navigation between different application views.
  • To display components according to the current URL.
  • To create multi-page experiences within a React application.
  • To support dynamic URLs such as product or student details.
  • To manage nested pages and not-found routes.
  • To navigate programmatically after actions such as form submission.

3. How React Router Works

React Router checks the current URL and matches it with a configured route. When a route matches, its associated component is rendered.

User Opens a URL
↓
React Router Checks the Path
↓
Matching Route Is Found
↓
Corresponding Component Is Displayed

4. Important React Router Components and Hooks

BrowserRouter

BrowserRouter provides routing functionality based on the browser's URL and history.

Routes

Routes groups the route definitions used by the application.

Route

Route defines which component should be displayed for a particular URL path.

Link

Link provides navigation between routes without requiring a normal full-page link navigation.

NavLink

NavLink is useful when navigation links need styling based on whether their route is currently active.

useNavigate

The useNavigate hook allows a component to navigate to another route in response to an event or application action.

useParams

The useParams hook reads dynamic parameters from the current matched route.

5. Example of Route Definitions

The following example demonstrates the basic structure of routes using React Router. We will configure BrowserRouter and install the required package in upcoming lessons.

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

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

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

function App() {
    return (
        <BrowserRouter>
            <Routes>
                <Route path="/" element={<Home />} />
                <Route path="/about" element={<About />} />
            </Routes>
        </BrowserRouter>
    );
}

export default App;

Expected Behavior

  • Opening / displays the Home Page.
  • Opening /about displays the About Page.

Note: This example assumes React Router is installed and the application is configured to support these routes.

6. React Router Navigation Example

React Router provides the Link component for moving between routes. Unlike a regular anchor navigation, Link performs client-side navigation within the configured router.

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

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

Clicking Home navigates to the root route, while clicking About navigates to the /about route. The matching route must be configured in the application.

7. React Router Project Structure

A simple React application using React Router can organize page components and routing configuration separately.

src/
  components/
    Navigation.jsx
  pages/
    Home.jsx
    About.jsx
    Contact.jsx
  App.jsx
  main.jsx

This is an example structure. You can adjust it according to the size and requirements of your project.

Practice Tasks

  1. Define React Router in your own words.
  2. Write any five advantages of using React Router.
  3. Explain the purpose of BrowserRouter, Routes, and Route.
  4. Write a route definition for a Courses component.
  5. Create Link elements for Home, About, and Contact pages.
  6. Explain the difference between Link and NavLink.
  7. Describe how useNavigate and useParams are used.

Summary

React Router is a library that manages navigation and URL-based views in React applications. BrowserRouter provides browser routing, Routes groups route definitions, and Route connects URL paths with components. Link and NavLink support navigation, while useNavigate and useParams help with programmatic navigation and dynamic route parameters. In the next lesson, we will learn how to install React Router in a React project.