Here is my current React Router implementation:
const router = createBrowserRouter([
{
path: "/",
element: (
<Page activeNav="home" >
<Home />
</Page>
)
},
{
path: "/about",
element: (
<Page activeNav="about" >
<About />
</Page>
)
},
{
path: "/blog",
element: (
<Page activeNav="blog">
<Blog />
</Page>
)
},
{
path: "/blog/:postName",
element: (
<Page activeNav="blog" >
<Post />
</Page>
),
loader: ({ params }) => params.postName
},
{
path: "/chess",
element: <ChessRouter />
}
])
The last route, /chess
is of importance. I am looking to define routes such as /chess/play
, /chess/login
, /chess/register
, etc. My initial idea was to just put another Router
as the element for the /chess
path and then all those paths would be routed from there. However, that throws an error saying:
You cannot render a <Router> inside another <Router>. You should never have more than one in your app.
I also tried using the children property on the /chess
route but this does not render anything when I go to /chess/play
etc.
What is the correct way of implementing subpaths (not sure of the correct word for it)?
CodePudding user response:
It is correct that you cannot render a router component within another router component as this is an invariant violation. You need only one router and routing context per React app.
To render sub-routes on "/chess"
then there are two options:
Render nested routes in the routes configuration declaration. This requires the
ChessRouter
component to render anOutlet
component for nested routes to render theirelement
content into. Nested routes will be able to use the new RRDv6.4 Data APIs.const router = createBrowserRouter([ ..., { path: "/chess", element: <ChessRouter />, children: [ ..., { path: "play", element: <ChessPlay /> }, ... other chess sub-rotues ], } ]);
const ChessRouter = () => { ... return ( ... <Outlet /> ... ); };
Render a root route with trailing wildcard (
"*"
) character that allows descendent routes to also be matched. This allows theChessRouter
component to render descendent routes, i.e. aRoutes
component with a set ofRoute
components. Descendent routes will not be able to use the RRDv6.4 Data APIs.const router = createBrowserRouter([ ..., { path: "/chess/*", element: <ChessRouter />, } ]);
const ChessRouter = () => { ... return ( ... <Routes> ... <Route path="/play" element={<ChessPlay />} /> ... other chess sub-rotues </Routes> ... ); };