React Router is a library for React that allows you to easily handle client-side routing. It helps you to navigate between different pages in your application without having to refresh the entire page.
Here is an example of how to use React Router:
First, you need to install React Router by running the following command: npm install react-router-dom
Next, you need to import the BrowserRouter
component from the react-router-dom
library in your index.js
file:
Wrap your entire app component in the BrowserRouter
component:
Define your routes by creating a component for each page in your application. For example, to create a route for the homepage, you would do the following:
To navigate to different routes in your application, you can use the
component from the react-router-dom
library. For example, to create a link to the homepage, you would do the following:
And that's it! You now have a basic setup for client-side routing in your React application using React Router. This is just a basic example, but React Router also has many other features such as dynamic routing, protected routes, and more.