How to go back to previous route in react-router-dom v6

Total
0
Shares

On early versions we can go back to previous route using history.

history.goBack()

How I can achieve that with v6 of react-router-dom?


Solution

Try this approach

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

function YourApp() {
  const navigate = useNavigate();

  return (
    <>
      <button onClick={() => navigate(-1)}>go back</button>
    </>
  );
}

Source: StackOverflow.com

Leave a Reply

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