back button next js

Step 1: Import the 'useRouter' hook from 'next/router'.

import { useRouter } from 'next/router';

Step 2: Inside the functional component, initialize the router using the 'useRouter' hook.

const router = useRouter();

Step 3: Create a function to handle the back button action using the 'router.back()' method.

const handleBackButtonClick = () => {
  router.back();
};

Step 4: Render a button or any clickable element in your component and attach the 'handleBackButtonClick' function to its 'onClick' event.

<button onClick={handleBackButtonClick}>Go Back</button>

Now, clicking the button will trigger the 'router.back()' method, taking the user to the previous page in the browsing history.