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

Routes and Route Components

In this lesson, we will learn how Routes and Route work together to display the correct React component for a particular URL.

1. What Is a Route?

A Route is a React Router component that associates a URL path with an element to render when that route matches the current location.

For example, the path /about can display an About component, while /courses can display a Courses component.

Remember: Route defines the path and the component that should be displayed for that path.

2. What Is Routes?

Routes is a component that groups Route definitions and renders the best matching route for the current URL.

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

In this example, Routes contains two route definitions: one for the Home page and another for the About page.

3. How Routes and Route Work Together

Current Browser URL
↓
Routes
↓
Match the URL with a Route
↓
Render the Matching Element

React Router compares the current URL with the configured route paths. When a route matches, its element is rendered.

4. Understanding the path and element Props

path

The path prop specifies the URL pattern that the route should match.

element

The element prop specifies the React element that should be rendered when the route matches.

<Route
    path="/courses"
    element={<Courses />}
/>

Here, the Courses component is rendered when the /courses route matches.

5. Create Page Components

First, create the components that represent the pages of your application.

function Home() {
    return <h2>Welcome to CIIT Training Institute</h2>;
}

function About() {
    return <h2>About CIIT Training Institute</h2>;
}

function Courses() {
    return <h2>Our Training Courses</h2>;
}

function Contact() {
    return <h2>Contact Us</h2>;
}

Each component represents a separate view in the React application.

6. Configure Multiple Routes

Import Routes and Route from React Router and define the required paths inside the application's existing BrowserRouter.

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

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

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

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

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

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

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

export default App;

Important: This App example must be rendered inside the BrowserRouter configured in the previous lesson. Do not add a second BrowserRouter around the same application.

7. Expected Output

When the browser opens the root URL:

CIIT Training Institute
Home Page

When the browser opens /about:

CIIT Training Institute
About Page

When the browser opens /courses:

CIIT Training Institute
Courses Page

When the browser opens /contact:

CIIT Training Institute
Contact Page

8. Add a Not Found Route

You can define a fallback route using the wildcard path *. It matches a URL that does not match another configured route.

<Routes>
    <Route path="/" element={<Home />} />
    <Route path="/about" element={<About />} />
    <Route path="*" element={<h2>Page Not Found</h2>} />
</Routes>

For example, an unconfigured URL such as /unknown displays Page Not Found.

We will explore not-found routes in detail in a separate lesson later in this module.

Practice Tasks

  1. Explain the difference between Routes and Route.
  2. Explain the purpose of the path prop.
  3. Explain the purpose of the element prop.
  4. Create Home, About, Courses, and Contact components.
  5. Configure routes for all four components.
  6. Test each URL in the browser.
  7. Add a wildcard route for unknown URLs.
  8. Write the expected output for each configured route.

Summary

Routes groups route definitions, while each Route associates a URL path with the React element to display when that route matches. The path prop specifies the URL pattern, and the element prop specifies the component to render. Multiple routes can represent different application pages, and a wildcard route can handle unmatched paths. In the next lesson, we will learn navigation using Link and NavLink.