Create a Back button with React Router

To create a back button with React Router use useNavigate() hook. We can navigate to the previous page programmatically by using the useNavigate hook. Call navigate function with an argument -1 eg. navigate(-1); inside the onClick event handler function of the back button.

In this article, we are going to discuss this with an example code. Besides the browser’s back button, our custom back button will add convenience and optimize the user experience.

To create a back button using useNavigate() hook with React Router follow the below steps.

  1. Set the onClick event handler function for a button.
  2. Use the useNavigate() hook, eg. const navigate = useNavigate();.
  3. Call navigate function with -1 as an argument inside the event handler function. eg. navigate(-1)

Let us discuss this with below example code.

import React from 'react';
import {Route, Link, Routes, useNavigate} from 'react-router-dom';

export default function App() { 
  return (
		<>
			<ul>
				<li>
					<Link to="/">Home</Link>
				</li>
				<li>
					<Link to="/about">about</Link>
				</li>
			</ul>
			<Routes>		
				<Route path="/about" element={<About/>} />
				<Route path="/" element={<Home/>} />
			</Routes>
		</>
	);
}

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

function About() {
	const navigate = useNavigate();
	const goBack = () => {
		navigate(-1);
	}
	return <>	
		<button onClick={goBack}>Back</button>	
		<p>About Page</p>
	</>;
}

In the above code, we imported useNavigate from react-router-dom. The useNavigate hook returns a function that lets us navigate programmatically. By calling navigate(-1);the page will redirect to the previous page. Similarly, If we want to go back to more than one page, replace -1 with -2, -3, etc.

If we want to move one page forward, we can call navigate(1);.

To use useNavigate() hook, make sure to wrap the entire App component with the BrowserRouter component as given below.

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { BrowserRouter } from 'react-router-dom';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  	<React.StrictMode>
    	<BrowserRouter>
      		<App />
    	</BrowserRouter>
  	</React.StrictMode>
);

reportWebVitals();

In the above code, we imported BrowserRouter from react-router-dom and wrapped the entire App component with this BrowserRouter component. Since we are wrapping the entire App Component with the BrowserRouter in index.js, we can use any react-router-dom hooks or components anywhere in the application.

Similarly, we can use useNavigate() hook to navigate to another page on a button click or any other event. In the below example, we are navigating to the product page with a button click.

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

function About() {
	const navigate = useNavigate();
	const clickHandler = () => {
		navigate('/product', {replace: true});
	}
	return <>	
		<button onClick={clickHandler}>Go to product</button>	
		<p>About Page</p>
	</>;
}

The second argument {replace: true} is for the current entry in the history stack to get replaced with the new URL. This means, the browser history was replaced with a new URL and when the user clicks on the back button it will not redirect to the previous page.

Conclusion

To create a back button with React Router, use the useNavigate() hook from react-router-dom. 

Use the useNavigate() hook, eg. const navigate = useNavigate();. Call navigate function with -1 as an argument eg. navigate(-1);  when the back button was clicked. We can use useNavigate() hook to navigate to any other pages programmatically. Make sure to wrap the App component with BrowserRouter imported from react-router-dom.