How To Create a Back button with React Router

How To Create a Back button with React Router

This article will show you how to create a Back button with React Router. Here are some ways to do it with examples to make it easier for readers to understand. Let’s go into detail how to use useHistory() and useNavigate().

React Router hook

The lightweight library React Router makes it possible to manage and handle routing for your React application.

The Route renders methods are one of React Router’s most notable features. It gives your component access to the matches, history, and location of props. You can use these props to navigate your React app and pass data from the URL to the component.

A few hooks in React Router let you access the router’s state and navigate within your components. Kindly note: To use the following hooks, you must use React version earlier than 16.8.

  • useHistory
  • useLocation 
  • useParams 
  • useRouteMatch

The usehistory hook provides access to the history instance, which can be used to navigate import use history from “react-router-dom”.

The useLocation hook returns a location object that represents the current URL. It’s similar to useState, which updates the location whenever the URL changes.

The useParams hook returns an object containing the key/value pairs of the URL parameter, which can be extremely helpful. You can access the match, params of the current Route by utilizing it.

Similar to Route, the useRouteMatch hook matches the current URL. Access to data not rendered at the Route is primarily practical.

Create a Back button with React Router

In the examples, we will show you the code in the component after the page has moved, which is the component with a Back button to go back to the previous page.

Use useHistory()

With this useHistory method, we can navigate the web page to the page we need. When we use the goBack() method, we can return to the page right before the page we are currently on.

Code:

const HomePage = () => {
    let history = useHistory();
    return (
        <div>
            <button onClick={() => { history.goBack(); }} >
              	Back
            </button>
        </div>
    );
};
 
export default App;

Use useNavigate()

In the “react-router-dom” v6 version, useHistory() has been removed, and you have to use an alternative method, useNavigate(), to navigate between the pages. Take a look at the example below to learn more about this.

Code:

const HomePage = () => {
    const navigate = useNavigate();
    const goBack = () => {
      	navigate(-1);
    };

    return (
        <div>
            <button onClick={goBack}}>
            	Back
            </button>
        </div>
    );
};

export default App;

When the button is clicked, we will run the callback function to be able to run the navigation. The code using navigate(-1) means returning to one unit and the previous page. I hope the above two methods can be applied to your program.

Summary

The article has shown you several ways to create a Back button with React Router. You can use useHistory() or useHistory() to do it. Good lucks for you!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *