0

I'm using react-router-domv6 and I wanna change the URL in order to change the route

We can use useNavigation() hook in a component but I want to use something similar in a redex action

Also history has a problem with react-router-dom V6

import { Route, BrowserRouter as Router, Routes } from "react-router-dom";

const App = () => {
  return (
    <div>
      <Router>
        <Routes>
          <Route path="/" element={<MainPage />} />
          <Route path="/second" element={<SecondPage />} />
        </Routes>
      </Router>
    </div>
  );
};

export default App;

my action is just a regular action

const action = () => {

      //Here I want to change the route

}
Sylhare
  • 5,907
  • 8
  • 64
  • 80
  • Does this answer your question? https://stackoverflow.com/questions/71945949/reactjs-redirect-to-dashboard-page-after-successful-login-with-react-router-dom/71946710#71946710 – Drew Reese May 04 '22 at 18:45
  • @DrewReese That's an idea too. But in previous version (v5) we could use `history` package with assigning `createCustomBrowserHistory()` to Router's history and simply import it and then use it with no need to pass it to every action that we want to change route inside. I wonder if there’s anything like that we could still use. – Johan Tenison May 04 '22 at 18:50
  • So you want something like this then? https://stackoverflow.com/a/70000286/8690857 – Drew Reese May 04 '22 at 18:52
  • That link was more about creating the router and custom history object in RRDv6, but this [answer](https://stackoverflow.com/a/70012117/8690857) has an example use case, just FYI. – Drew Reese May 04 '22 at 18:58
  • @DrewReese Yeah that’s it man! Thank you. Do you know the definition type for history property that the `Router` is getting? I'm using TypeScript. – Johan Tenison May 04 '22 at 19:19
  • I'm not a TS expert, but you could probably get them straight from [source](https://github.com/remix-run/history/blob/main/packages/history/index.ts). – Drew Reese May 04 '22 at 19:21

0 Answers0