import { useHistory } from "react-router-dom"; // DEPRECATED
// It is now
import { useNavigate } from "react-router-dom"; // As at March 3, 2022
import { useHistory } from 'react-router-dom';
function Home() {
const history = useHistory();
return <button onClick={() => history.push('/profile')}>Profile</button>;
}
// useHistory() does not work inside an arrow function
// notice @ line 9 that the history.push() is inside a usual function. not an arrow function
let myComponent = () => {
const history = useHistory();
function routeChange(){
history.push("/author");
}
return(<>
<button onClick={ routeChange} > redirect </button>
</>)
}
// useHistory() has been changed in v6, so instead use useNavigate()
check out the source link
replaced by useNavigate