Address
[wueseai_login_modal button_text="Sign In Now" button_class="px-6 py-3 bg-green-500 text-white rounded-full"]
Work Hours
Monday to Friday: 7AM - 7PM
Weekend: 10AM - 5PM
Address
[wueseai_login_modal button_text="Sign In Now" button_class="px-6 py-3 bg-green-500 text-white rounded-full"]
Work Hours
Monday to Friday: 7AM - 7PM
Weekend: 10AM - 5PM

Creating a multi-page website is a fundamental skill for any web developer. This article walks you through the process of building a course listing website with React, focusing on implementing navigation between pages using React Router. An accompanying video walks through the full process.
This guide demonstrates how to set up React Router to navigate between different course pages in a React application. It covers installing the necessary dependencies, configuring routes in `app.js`, and implementing navigation using the `useNavigate` hook. By the end of this tutorial, you’ll have a functional multi-page website where users can click on a course and be directed to a dedicated page for that course.
The first step in creating a multi-page website is to install React Router. React Router is a standard library for routing in React, enabling navigation between different views or components. To install it, use the following command in your project’s terminal:
npm install react-router-dom
Once the installation is complete, ensure your application is running correctly. Next, you’ll need to configure the routing in your index.js and app.js files.
In your index.js file, wrap your entire application with the BrowserRouter component. This component enables the use of routing features throughout your application. If it’s not already there, make sure to add the BrowserRouter.
javascript import { BrowserRouter } from ‘react-router-dom’;
// …
root.render(
);
Now, open your app.js file. Here, you’ll define the routes for your application. Import the necessary components from react-router-dom, such as Routes and Route. Wrap your routes within the <Routes> tag. Each route defines a specific path and the component that should be rendered when that path is accessed.
javascript import { Routes, Route } from ‘react-router-dom’; import Home from ‘./pages/Home’; import PlayVideo from ‘./pages/PlayVideo’;
function App() { return (
); }
export default App;
In this example, the default route ("/") renders the Home component. Additionally, a route for playing videos (/playvideo/:id) is defined. The :id is a dynamic parameter that allows you to pass the video ID in the URL.
Create a new file named PlayVideo.js in your pages directory. This component will be responsible for displaying the video and related information when a user clicks on a course. Start with a basic template for the component.
javascript import React from ‘react’;
function PlayVideo() { return (
); }
export default PlayVideo;
To enable navigation from the course list to the PlayVideo page, you’ll need to implement an onClick handler on each course item. This handler will use the useNavigate hook from React Router to navigate to the appropriate URL.
In your VideoGridList component (or wherever you are displaying the course list), import the useNavigate hook from react-router-dom. Inside the component, call the hook to get the navigate function.
javascript import { useNavigate } from ‘react-router-dom’;
function VideoGridList({ videos }) { const navigate = useNavigate();
// … }
Now, add an onClick handler to each course item. This handler should call the navigate function with the URL for the PlayVideo page, including the video ID as a parameter.
javascript function VideoGridList({ videos }) { const navigate = useNavigate();
const handleClick = (item) => {
navigate(/playvideo/${item.id});
};
return (
))}
</div>
); }
In this example, when a course item is clicked, the handleClick function is called with the course item as an argument. The navigate function is then called with the URL /playvideo/${item.id}, where item.id is the unique identifier for the video.
Make sure you pass the actual video ID from your data source (item.id). This ID will be used to fetch the video details on the PlayVideo page.
Here are the key takeaways from building a multi-page course listing website with React Router:
npm install react-router-dom to add React Router to your project.BrowserRouter in index.js to enable routing.<Routes> and <Route> components to define the paths and corresponding components.PlayVideo component to display video details.PlayVideo page using the useNavigate hook.PlayVideo page.Here are some frequently asked questions about React Router and building multi-page applications:
React Router is a standard library for routing in React. It enables navigation between different views or components in a React application, allowing you to build single-page applications with multiple routes.
You can install React Router using npm or yarn. Run the command npm install react-router-dom or yarn add react-router-dom in your project’s terminal.
The useNavigate hook is a function provided by React Router that allows you to programmatically navigate between different routes in your application. You can use it to redirect users to different pages based on certain actions or events.
You can pass parameters in a route by defining a dynamic segment in the path using a colon (:). For example, /playvideo/:id defines a route that accepts an ID parameter. You can then access this parameter using the useParams hook in the component rendered by the route.
This usually happens when you have multiple routes defined in your app.js file, and one of the routes is rendering a component that includes other components. Ensure that you only have one default route ("/") rendering the main component, and that all other routes are defined correctly with their respective paths and components.
Building a multi-page website with React and React Router involves setting up the necessary routing infrastructure, creating individual page components, and implementing navigation between them. By following the steps outlined in this guide, you can create a functional and user-friendly course listing website where users can easily browse and access different courses. Remember to handle data fetching and component rendering appropriately on each page to ensure a smooth and seamless user experience.
Credit: Creator
Credit: Writer
Credit: Reviewer