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 – Create Invalid Path Redirect

I want to make it so if the user enters an invalid path in the URL it automatically redirects them to the homepage.

Something like this:

<BrowserRouter>
    <Routes>
        <Route exact path="/" element={<Home />}/>
        <Route exact path="/page" element={<page />}/>
        <Route path="*" /*Go to path - "/" */ />
    </Routes>
</BrowserRouter>

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 :

Render a redirect as the routed component, in this case the Navigate component with the replace prop specified.

<Route path="*" element={<Navigate to="/" replace />} />
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