Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

history.replace in react-router-dom v6

I've previously used react-router-dom v5.2.0. There I used history.replace('/path) to redirect the page to another page. (Hence it will not store in the address history). Now I have to use react-router-dom v6.0.0-beta.0. In version 6, I have to use useNavigate hook instead of useHistory hook. I can use it as below.

const navigate = useNavigate();
navigate('/path')

But I don't know how to use it for redirect. (Like history.replace)

like image 748
Pranavan Avatar asked Sep 05 '25 13:09

Pranavan


1 Answers

If you need to replace the current location instead of push a new one onto the history stack, use navigate(to, { replace: true }). If you need state, use navigate(to, { state }).

like image 94
Ako Avatar answered Sep 08 '25 01:09

Ako