4

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?

Drew Reese
  • 165,259
  • 14
  • 153
  • 181
iCodeByte
  • 571
  • 1
  • 7
  • 21
  • This behavior works but for some reason, it just changes the route and doesn't load the component. I have been trying to find a replacement for the same. – Tanishq Vyas Nov 30 '21 at 10:11

1 Answers1

3

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

Tanishq Vyas
  • 1,422
  • 1
  • 12
  • 25