Home > database >  use navigate not working for the first time
use navigate not working for the first time

Time:09-28

I'm trying to create a back button with react

this is my function

<button onClick={() => navigate(-1)}>

But it is working only on the second click not the first one

CodePudding user response:

Im not an expert with react but maybe you can try it like this.

      <button onClick={() => history.goBack()}>Back</button>

CodePudding user response:

you can use "useHistory" hook.

import { useHistory } from 'react-router-dom';
const history = useHistory();

//call this method on button click
history.goBack();
  • Related