Lesson 42 of 60 – Routes and Links
70%

React Routes and Links

In React Router, Routes define which component should be displayed for a particular URL, while Link components allow users to navigate between those routes.

Note: React Router provides routing and navigation tools for React applications. Use Route to connect paths with components and Link to create navigation between routes.

1. What are Routes?

A Route defines what should be displayed when the browser URL matches a particular path.

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

Here, the /about path is connected to the About component.

2. What is Routes?

The Routes component is used to contain one or more Route definitions.

<Routes>

    <Route
        path="/"
        element={<Home />}
    />

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

</Routes>

3. Route Path

The path prop specifies the URL pattern for a route.

<Route
    path="/contact"
    element={<Contact />}
/>

The route above is associated with the /contact URL.

4. Route Element

The element prop specifies the React element that should be displayed for a matching route.

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

The About component is displayed when the route matches.

5. Creating a Home Route

The root path / is commonly used for the home page.

<Route
    path="/"
    element={<Home />}
/>

When the application opens at the root URL, the Home component can be displayed.

6. Creating an About Route

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

This route connects the /about path with the About component.

7. Creating a Contact Route

<Route
    path="/contact"
    element={<Contact />}
/>

The Contact component can be displayed when the user visits /contact.

8. Multiple Routes

A React application can contain multiple routes.

<Routes>

    <Route
        path="/"
        element={<Home />}
    />

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

    <Route
        path="/contact"
        element={<Contact />}
    />

</Routes>

9. What is Link?

The Link component is used to create navigation links in a React Router application.

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

<Link to="/about">
    About
</Link>

10. The to Prop

The to prop specifies the destination route for a Link.

<Link to="/contact">
    Contact
</Link>

When the user clicks the link, the application navigates to /contact.

11. Creating a Navigation Menu

A navigation menu can contain multiple Link components.

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

function Navbar() {

    return (
        <nav>

            <Link to="/">
                Home
            </Link>

            <Link to="/about">
                About
            </Link>

            <Link to="/contact">
                Contact
            </Link>

        </nav>
    );
}

12. Link to the Home Page

Use to="/" to create a link to the root route.

<Link to="/">
    Home
</Link>

13. Link to the About Page

<Link to="/about">
    About
</Link>

This link navigates the user to the /about route.

14. Link to the Contact Page

<Link to="/contact">
    Contact
</Link>

The destination is determined by the value of the to prop.

15. Link vs Anchor Tag

Traditional HTML navigation uses an anchor element:

<a href="/about">
    About
</a>

React Router provides:

<Link to="/about">
    About
</Link>

For internal React Router navigation, Link is commonly used.

16. Why Use Link?

The Link component is designed for navigation within a React Router application.

  • Connects to application routes.
  • Provides client-side navigation.
  • Works with React Router.
  • Can be used inside navigation menus.
  • Helps create SPA-style navigation.

17. Routes and Links Work Together

A Route defines where a URL should display a component, while a Link provides a way for the user to navigate to that URL.

<Link to="/about">
    About
</Link>

<Routes>

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

</Routes>

18. Navigation Bar Component

It is a good practice to create a reusable navigation component.

function Navbar() {

    return (
        <nav>

            <Link to="/">Home</Link>

            {" | "}

            <Link to="/about">About</Link>

            {" | "}

            <Link to="/contact">Contact</Link>

        </nav>
    );
}

19. Importing Link

Before using Link, import it from react-router-dom.

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

Without the correct import, React will not know what Link refers to.

20. Complete Routes Example

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

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

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

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

function AppRoutes() {

    return (
        <Routes>

            <Route
                path="/"
                element={<Home />}
            />

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

            <Route
                path="/contact"
                element={<Contact />}
            />

        </Routes>
    );
}

export default AppRoutes;

21. Complete Link Example

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

function Navbar() {

    return (
        <nav>

            <Link to="/">
                Home
            </Link>

            {" | "}

            <Link to="/about">
                About
            </Link>

            {" | "}

            <Link to="/contact">
                Contact
            </Link>

        </nav>
    );
}

export default Navbar;

22. Adding Navbar and Routes Together

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

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

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

function App() {

    return (
        <>

            <nav>

                <Link to="/">
                    Home
                </Link>

                {" | "}

                <Link to="/about">
                    About
                </Link>

            </nav>

            <Routes>

                <Route
                    path="/"
                    element={<Home />}
                />

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

            </Routes>

        </>
    );
}

23. Links in Different Components

Links do not have to be placed only in one navigation component. They can also be used in other components.

function Home() {

    return (
        <div>

            <h1>Home</h1>

            <Link to="/products">
                View Products
            </Link>

        </div>
    );
}

24. Links with Button-Like Styling

A Link can be styled with CSS classes to look like a button.

<Link
    to="/contact"
    className="btn btn-primary"
>
    Contact Us
</Link>

This is useful when Bootstrap is being used in a React application.

25. Navigation After a Form

A link can be displayed after a form or other content to provide navigation.

function Login() {

    return (
        <div>

            <h2>Login</h2>

            <form>
                <input
                    type="email"
                    placeholder="Email"
                />
            </form>

            <Link to="/register">
                Create an Account
            </Link>

        </div>
    );
}

26. Navigation to Dashboard

After authentication, an application may provide a link to a dashboard.

<Link
    to="/dashboard"
    className="btn btn-success"
>
    Go to Dashboard
</Link>

The corresponding route could be:

<Route
    path="/dashboard"
    element={<Dashboard />}
/>

27. Common Routes and Links Mistakes

  • Forgetting to import Link.
  • Using href when a React Router Link is intended.
  • Writing an incorrect path.
  • Forgetting the to prop.
  • Using Route outside Routes.
  • Using Link outside router context.
  • Forgetting to define the route for a destination.

28. Routes and Links Workflow

A simple workflow is:

1. Create a component
        ↓
2. Create a Route
        ↓
3. Assign a path
        ↓
4. Create a Link
        ↓
5. Set the Link's "to" value
        ↓
6. Click the Link
        ↓
7. Matching Route displays component

29. Complete Routes and Links Example

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

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

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

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

function App() {

    return (
        <BrowserRouter>

            <nav>

                <Link to="/">
                    Home
                </Link>

                {" | "}

                <Link to="/about">
                    About
                </Link>

                {" | "}

                <Link to="/contact">
                    Contact
                </Link>

            </nav>

            <Routes>

                <Route
                    path="/"
                    element={<Home />}
                />

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

                <Route
                    path="/contact"
                    element={<Contact />}
                />

            </Routes>

        </BrowserRouter>
    );
}

export default App;

30. Routes and Links Best Practices

  • Use meaningful route paths.
  • Use Link for internal React Router navigation.
  • Keep navigation menus reusable.
  • Keep route definitions organized.
  • Use descriptive page component names.
  • Make sure every important Link has a matching route.
  • Test all navigation links.
  • Use CSS or Bootstrap classes to make navigation user-friendly.
  • Keep routes easy to understand.
  • Handle invalid or missing routes in larger applications.
Link
 ↓
URL
 ↓
Route
 ↓
Component

📌 Key Points

  • Routes contains route definitions.
  • Route connects a URL path with a React element.
  • Link creates navigation links.
  • The to prop specifies the destination of a Link.
  • Routes and Links work together to create application navigation.
  • Use Link for internal navigation in React Router applications.
  • Every important navigation link should have a corresponding route.

🧠 Quick Quiz

Question: Which React Router component is commonly used to navigate between routes?