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

Navigation with Link and NavLink

In this lesson, we will learn how to navigate between React routes using Link and NavLink, understand their differences, and create a navigation menu with active-link styling.

1. What Is Navigation in React?

Navigation means moving from one page or view to another within an application. For example, a user may move from the Home page to the Courses page or from the Courses page to the Contact page.

React Router provides the Link and NavLink components to support navigation between configured routes. They allow users to move between views without requiring a normal full-page navigation for each internal route change.

Home Page
↓ Click Courses Link
Courses Page
↓ Click Contact Link
Contact Page

Remember: The destination URL must have a matching route definition for the appropriate page component to appear.

2. What Is the Link Component?

Link is a component provided by React Router. It creates a navigation link that changes the current route when the user clicks it.

The to prop specifies the destination path. The path should correspond to a route configured in the application.

Syntax

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

<Link to="/courses">
    View Courses
</Link>

Explanation

  • Link: The React Router navigation component.
  • to: Specifies the destination URL path.
  • View Courses: The text displayed to the user.

When the user clicks View Courses, React Router navigates to /courses and renders the matching route if one is configured.

3. Create a Navigation Menu Using Link

Consider a CIIT Training Institute website containing Home, About, Courses, and Contact pages. We can use Link to create a simple navigation menu.

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

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

export default Navigation;

Expected Output

Home | About | Courses | Contact

The output displays four navigation links. Clicking each link changes the URL to its corresponding path.

Note: This code defines the navigation component. Render it inside your application to display the menu, and make sure all four paths have route definitions.

4. What Is the NavLink Component?

NavLink is similar to Link, but it also provides information about whether its destination matches the current route. This makes it useful for navigation menus where the active page should look different.

For example, when the user is on the Courses page, the Courses navigation link can appear blue and bold, while the other links retain their normal style.

Basic Syntax

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

<NavLink to="/courses">
    Courses
</NavLink>

NavLink automatically supplies an isActive value to its function-based className or style callback. We can use that value to apply active styling.

5. Apply Active Styling with NavLink

We can use the className callback to assign a CSS class when a navigation link is active.

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

function Navigation() {
    return (
        <nav>
            <NavLink
                to="/"
                className={({ isActive }) =>
                    isActive ? 'active-link' : 'nav-link'
                }
            >
                Home
            </NavLink>

            <NavLink
                to="/courses"
                className={({ isActive }) =>
                    isActive ? 'active-link' : 'nav-link'
                }
            >
                Courses
            </NavLink>
        </nav>
    );
}

export default Navigation;

CSS Styling

.nav-link {
    color: #374151;
    text-decoration: none;
    padding: 8px 12px;
}

.active-link {
    color: #2563eb;
    font-weight: bold;
    text-decoration: underline;
}

How It Works

  1. NavLink checks whether its route is active.
  2. The callback receives the isActive value.
  3. If isActive is true, the active-link class is applied.
  4. Otherwise, the nav-link class is applied.

For the root path /, NavLink normally uses root matching behavior so it does not remain active for every route. You can explicitly use end when you want exact end matching.

6. Complete Example Using Link and NavLink

The following example demonstrates a navigation menu with active styling and multiple route components. It assumes React Router is installed and BrowserRouter is configured once around the application.

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

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

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

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

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

            <nav>
                <NavLink
                    to="/"
                    end
                    className={({ isActive }) =>
                        isActive ? 'active-link' : 'nav-link'
                    }
                >
                    Home
                </NavLink>

                {' | '}

                <NavLink
                    to="/courses"
                    className={({ isActive }) =>
                        isActive ? 'active-link' : 'nav-link'
                    }
                >
                    Courses
                </NavLink>

                {' | '}

                <NavLink
                    to="/about"
                    className={({ isActive }) =>
                        isActive ? 'active-link' : 'nav-link'
                    }
                >
                    About
                </NavLink>
            </nav>

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

export default App;

CSS for Active Navigation

.nav-link {
    color: #374151;
    text-decoration: none;
    padding: 8px 12px;
}

.active-link {
    color: #2563eb;
    font-weight: bold;
    text-decoration: underline;
}

Expected Behavior

  • Opening / displays the Home page and activates Home.
  • Opening /courses displays Courses and activates Courses.
  • Opening /about displays About and activates About.
  • Clicking a navigation link changes the displayed route.

7. Difference Between Link and NavLink

Feature Link NavLink
Navigation Supports navigation between routes. Supports navigation between routes.
Active route information Does not provide NavLink's isActive callback. Provides isActive information.
Active styling Must be managed separately if needed. Can apply styling based on the active route.
Common use Regular links within the application. Navigation menus and active page indicators.

Easy rule: Use Link for ordinary navigation. Use NavLink when you need active-route styling or active-state information.

8. Common Mistakes

Mistake 1: Using the wrong destination prop

// Incorrect
<Link href="/courses">Courses</Link>

// Correct
<Link to="/courses">Courses</Link>

React Router Link uses the to prop to specify the destination, not href.

Mistake 2: Missing route definition

If a link points to /courses but no matching route exists, the expected Courses component will not appear. Define the corresponding Route.

Mistake 3: Using Link outside the router

Components using Link or NavLink must be rendered within the configured router.

Mistake 4: Expecting automatic active styling from Link

Link does not automatically supply NavLink's isActive callback. Use NavLink when active-state styling is required.

Practice Tasks

  1. Create a navigation menu with Home, About, Courses, and Contact.
  2. Use Link to navigate between the configured routes.
  3. Replace the navigation links with NavLink.
  4. Apply a blue color to the active navigation link.
  5. Make the active link bold and underlined.
  6. Use the end prop on the Home NavLink.
  7. Explain the difference between Link and NavLink.
  8. Correct an example that incorrectly uses href on Link.

Summary

Link and NavLink are React Router components used to navigate between configured routes. Link provides ordinary client-side navigation through its to prop, while NavLink also exposes active-route information through isActive. This makes NavLink useful for highlighting the current page in navigation menus. Always configure matching routes and render navigation components inside the router. In the next lesson, we will learn programmatic navigation using the useNavigate hook.