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

Nested Routes in React

Learn how to create nested routes using React Router, display child pages inside a parent layout, use the Outlet component, and organize related pages with practical examples at CIIT Training Institute.

React Router Practical Examples Expected Output Included

01 What Are Nested Routes?

Nested routes are routes defined inside another route. They are useful when multiple pages belong to one common section of an application and need to share the same layout.

For example, a student dashboard may contain Profile, Courses, and Results pages. All these pages belong to the Dashboard section. Instead of creating a separate dashboard layout for each page, we can create one parent route and define the related pages as its child routes.

Remember: A parent route defines the common page layout, and its child routes display the related content inside that layout.
CIIT TRAINING INSTITUTE — NESTED ROUTE STRUCTURE
Dashboard Parent Route: /dashboard
↓
Profile /dashboard/profile
Courses /dashboard/courses
Results /dashboard/results

In this structure, Dashboard is the parent route. Profile, Courses, and Results are child routes. When the user selects a child page, its content changes while the parent layout can remain visible.

02 Why Do We Need Nested Routes?

In real applications, several pages often share a header, sidebar, navigation menu, or other common UI elements. Nested routes help us keep these common elements in a parent component and display different child pages inside it.

  • They organize related pages under a parent route.
  • They allow multiple pages to reuse a common layout.
  • They reduce repeated layout code.
  • They create a clear URL hierarchy.
  • They make routing easier to understand and maintain.
Example URL Structure ROUTING
/dashboard
/dashboard/profile
/dashboard/courses
/dashboard/results
Example Application Layout

Student Dashboard

Profile Courses Results

The dashboard heading and navigation are common elements. The selected child page appears below them.

03 Understanding the Outlet Component

React Router provides the Outlet component to display the matched child route inside its parent route. It acts as the location where the child component will be rendered.

Suppose Dashboard contains a heading and navigation menu. If the user opens the Courses page, the Dashboard component renders its common layout and Outlet displays the Courses component below the navigation menu.

HOW OUTLET WORKS
Dashboard Component Common heading and navigation
↓
<Outlet /> Placeholder for the matched child route
↓
Courses Component Child content for /dashboard/courses
Dashboard.jsx JSX
import { Outlet, Link } from "react-router-dom";

function Dashboard() {
    return (
        <div>
            <h1>Student Dashboard</h1>

            <nav>
                <Link to="profile">Profile</Link>
                {" | "}
                <Link to="courses">Courses</Link>
                {" | "}
                <Link to="results">Results</Link>
            </nav>

            <hr />

            <Outlet />
        </div>
    );
}

export default Dashboard;

Explanation:

  • Outlet is imported from react-router-dom.
  • Link creates navigation links for the child pages.
  • to="courses" navigates to the Courses child route relative to the current parent route.
  • Outlet displays whichever child route matches the current URL.
Important: Defining child routes alone is not enough to display their content inside the parent layout. The parent component must render Outlet at the intended location.

04 Creating Child Route Components

Now create separate components for Profile, Courses, and Results. Each component contains the content of its own page. Keeping these components separate makes the application easier to organize and update.

Profile.jsx JSX
function Profile() {
    return (
        <div>
            <h2>Student Profile</h2>
            <p>Welcome to your profile page.</p>
        </div>
    );
}

export default Profile;
Expected Browser Output

Student Profile

Welcome to your profile page.

Courses.jsx JSX
function Courses() {
    return (
        <div>
            <h2>My Courses</h2>
            <p>View your enrolled courses here.</p>
        </div>
    );
}

export default Courses;
Expected Browser Output

My Courses

View your enrolled courses here.

Results.jsx JSX
function Results() {
    return (
        <div>
            <h2>My Results</h2>
            <p>View your examination results here.</p>
        </div>
    );
}

export default Results;
Expected Browser Output

My Results

View your examination results here.

05 Configuring Nested Routes

After creating the parent and child components, configure them in the main application. Place the child Route elements inside the parent Route element.

App.jsx JSX
import {
    BrowserRouter,
    Routes,
    Route
} from "react-router-dom";

import Dashboard from "./Dashboard";
import Profile from "./Profile";
import Courses from "./Courses";
import Results from "./Results";

function App() {
    return (
        <BrowserRouter>
            <Routes>
                <Route
                    path="/dashboard"
                    element={<Dashboard />}
                >
                    <Route
                        path="profile"
                        element={<Profile />}
                    />

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

                    <Route
                        path="results"
                        element={<Results />}
                    />
                </Route>
            </Routes>
        </BrowserRouter>
    );
}

export default App;

Step-by-step explanation:

  1. BrowserRouter: Enables browser-based routing for the application.
  2. Routes: Groups the route definitions.
  3. Parent Route: The path /dashboard renders the Dashboard component.
  4. Child Routes: The relative paths profile, courses, and results define the dashboard's child pages.
  5. Outlet: Displays the matched child component inside Dashboard.
Notice: The child paths do not start with a slash. They are relative paths, so React Router combines them with the parent path. For example, courses becomes /dashboard/courses.

06 Understanding the URL and Rendering Flow

When the browser opens a nested URL, React Router matches both the parent route and the appropriate child route. The parent renders its layout, and the matching child appears through Outlet.

EXAMPLE: /dashboard/courses
Browser URL /dashboard/courses
↓
Parent Route: Dashboard Renders the common dashboard layout
↓
Child Route: Courses Matched and rendered through Outlet
Expected Browser Output for /dashboard/courses

Student Dashboard

Profile Courses Results

My Courses

View your enrolled courses here.

The dashboard heading and navigation belong to the parent component. The "My Courses" content belongs to the child Courses component. When the user navigates to another child URL, the matching child content changes.

07 Creating a Default Child Route

When a user visits /dashboard without selecting a child page, we may want to display a welcome message. React Router supports an index route for this purpose.

An index route renders at the parent's URL. It does not add another segment to the URL.

App.jsx — Index Route JSX
<Route path="/dashboard" element={<Dashboard />}>
    <Route
        index
        element={
            <div>
                <h2>Welcome to Dashboard</h2>
                <p>Select a section to continue.</p>
            </div>
        }
    />

    <Route path="profile" element={<Profile />} />
    <Route path="courses" element={<Courses />} />
    <Route path="results" element={<Results />} />
</Route>
Expected Browser Output for /dashboard

Student Dashboard

Profile Courses Results

Welcome to Dashboard

Select a section to continue.

Remember: Use an index route for the default child content. The parent component must still render <Outlet /> to display it.

08 Common Mistakes in Nested Routing

While creating nested routes, beginners can make a few common mistakes. Understanding them helps you troubleshoot routing problems more easily.

Common Mistake: Missing Outlet INCORRECT
function Dashboard() {
    return (
        <div>
            <h1>Student Dashboard</h1>
        </div>
    );
}

The parent component does not contain Outlet, so the child page will not appear inside this layout.

Correct Dashboard.jsx JSX
import { Outlet } from "react-router-dom";

function Dashboard() {
    return (
        <div>
            <h1>Student Dashboard</h1>
            <Outlet />
        </div>
    );
}

export default Dashboard;

Other mistakes to avoid:

  • Defining child routes outside the intended parent route.
  • Using a child URL that does not match the route configuration.
  • Forgetting to import the required components.
  • Forgetting to add an index route when default content is needed.
  • Assuming Outlet automatically creates navigation links.
Important: Outlet displays child content; Link provides navigation. They have different responsibilities.

09 Practice Tasks

Complete the following tasks to practise nested routing using React Router.

Task 1: Create an Admin Dashboard

Create an AdminDashboard parent component with an Outlet. Add child routes for Users, Reports, and Settings.

Task 2: Create a Course Dashboard

Create a Courses parent page with child routes for Web Development, Data Analytics, and Software Testing.

Task 3: Add an Index Route

Display a welcome message when a user visits the parent dashboard URL without selecting a child page.

Task 4: Add Navigation Links

Use Link components to navigate between all child pages. Confirm that the parent layout remains visible while the child content changes.

Task 5: Test the URL Structure

Test the URLs for each child route and verify that the correct component is displayed through Outlet.

Summary

Nested routes allow related pages to share a common parent layout in React Router. Child Route elements are defined inside a parent Route, and the Outlet component displays the matching child content. Relative child paths create URLs such as /dashboard/profile and /dashboard/courses. An index route displays default content at the parent URL. Using nested routes helps organize application pages, reuse layouts, and maintain a clear routing structure.