简体   繁体   中英

useNavigate outside functional component - react-router v6

Is there a way to use useNavigate() hook outside functional component in react-router v6? I'm trying to use navigate(url) inside a saga function. I know in v4 we could do something like this:

const history = createBrowserHistory();
function* sagaFunc(){
 history.push(url)
}

Can we pass useNavigate() hook to do something similar outside component in react-router v6?

You can make use of to be able to use some routing functionality outside the react component.

window.history.pushState({}, "Dashboard", "/dashboard");
window.location.reload();

I myself haven't found any suitable replacement for the createBrowserHistory() which existed in v4 and v5 .

Reference taken from here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM