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

ReactJs: Redirect automatically when hitting a specific url is hit

I am new at react.js and I am trying to redirect the to /movies if the url is /.

I found some ways but they were either beyond my current understanding or not react-ish.

App.tsx

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

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

import Layout from "./components/layouts/Backend";
import Movies from "./pages/movies/Index";

...

export default () => (
    <BrowserRouter>
        <Routes>
            <Route path="/" element={<Layout />}>
                <Route path="movies" element={<Movies />} />

                ...
            </Route>
        </Routes>
    </BrowserRouter>
);

Currently, an empty layout is loaded whenever I enter http://localhost:5173. Therefore, I have to manually click the Movies link to redirect.

I want this to redirect to http://localhost:5173/movies automatically.

>Solution :

Adding an index route with Navigate component should do the trick

<Route
  index
  element={ <Navigate to="/movies" /> } 
/>
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