Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React Router V6 – Redirect inside Route Component

I have the following code from the V5 Documentation:

    <Route exact path="/">
      {loggedIn ? <Redirect to="/dashboard" /> : <PublicHomePage />}
    </Route>

I know that I have Navigate instead of Redirect in V6, but Navigate seems not to work the exact same way:

    <Route></Route>
      {loggedIn ? <Navigate to="/dashboard" /> : <PublicHomePage />}
    </Route>

Uncaught Error: [Navigate] is not a <Route> component.

How can I archieve this with React Router V6?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In react-router v6 <Route> it’s the only component that’s able to be child of <Routes>:

So change this logic:

<Route>
    {loggedIn ? <Navigate to="/dashboard" /> : <PublicHomePage />}
</Route>

to this:

<Route path="/" element={loggedIn ? <Navigate to="/dashboard" /> : PublicHomePage}

You can also checkout this article: Private Route in react-router v6

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading