I have a navigate back button and while checking for returning back I found two ways:
- The native way:
window.history.back()
or
window.history.go(-1)
- The react router way:
export const BackButton: FC = () => {
const navigate = useNavigate();
return (
<button
onClick={() => navigate(-1)}>
Back
</button>
);
}
or for v5
export const BackButton: FC = () => {
const { goBack } = useHistory();
return (
<button
onClick={goBack}>
Back
</button>
);
}
So I am wondering what is the differences between these ways.
I am guessing (didn't check the source code) that under the hood react router use the native way and do a bit more things, and if I am using react router then I should use the react router way but I still would like to understand the differences.
Thanks!