I would like it to work in the form of the 3rd route. Previously, if it was used, Navigate would not be called to display the Navbar on the page of the component, but if Homepage or Product was called, the Navbar would initially be called. I tried creating it in the manner below by storing my Navbar and Components in an Array, but got a warning, so I tried to find new ways.
import {
createBrowserRouter,
createRoutesFromElements,
Route,
} from "react-router-dom";
//pages
import ErrorPage from "@/pages/ErrorPage";
import LoginPage from "@/pages/LoginPage";
import RegisterPage from "@/pages/RegisterPage";
import Navigate from "@/components/Navigate";
import HomePage from "@/pages/HomePage";
import Products from "@/pages/Products";
const element = createRoutesFromElements(
<>
<Route path="*" element={<ErrorPage />} />
<Route path="/" element={<LoginPage />} />
<Route path="/register" element={<RegisterPage />} />
> I want to run the Navigate Component at once to be able to use it in the Homepage and Product Component.
<Route path="/homepage" element={[<Navigate />, <HomePage />]} />
<Route path="/products" element={[<Navigate />, <Products />]} />
</>
);
export const router = createBrowserRouter(element);
2
Answers
My new code version
router.tsx
layout.tsx
Navigation.tsx
An
<Outlet/>
should be used in parent route elements to render their child route elements. This allows nested UI to show up when child routes are rendered.First thing you have to create an parent component, I am calling it
Layout
and wrap the common components like nav bar, footer.Layout.js
Then you have to create a route as normally and apply
index
prop to render default child route.In
router