I am learning React and trying to build a project which is a simple house listing website. Here I am navigating to the top menu "List Room" which is showing in the image and then when the user clicks on the next button I want to go to the List Room 2 component which is also happening but the data of the parent listroom is showing how can I hide this?
<Routes>
<Route path = "/" element = { <MainMenu />}>
<Route index element = { Home />} />
<Route path = "/listrooms" element = { <ListRooms />}>
<Route path = "listrooms2" element = { <ListRooms2 />} />
</Route>
<Route path = "/login" element = { <Login />} />
<Route path = "*" element = { <ErrorPage />} />
</Route>
</Routes>
If you want ListRooms2
to be rendered on the nested "/listrooms/listrooms2"
path and not render ListRooms
at the same time then move ListRooms
to its own index route to only be rendered when the path is "/listrooms"
.
<Routes>
<Route path="/" element={<MainMenu />}>
<Route index element={<Home />} />
<Route path="/listrooms">
<Route index element={<ListRooms />} />
<Route path="listrooms2" element={<ListRooms2 />} />
</Route>
<Route path="/login" element={<Login />} />
<Route path="*" element={<ErrorPage />} />
</Route>
</Routes>
Alternatively you could unnest the routes, e.g. render each individually, using the complete absolute path.
<Routes>
<Route path="/" element={<MainMenu />}>
<Route index element={<Home />} />
<Route path="/listrooms" element={<ListRooms />} />
<Route path="/listrooms/listrooms2" element={<ListRooms2 />} />
<Route path="/login" element={<Login />} />
<Route path="*" element={<ErrorPage />} />
</Route>
</Routes>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments