I get this warning when I wrapped my component in <Routes>
.
Warning: [Categories] is not a
<Route>
component. All component
children of<Routes>
must be a<Route>
or<React.Fragment>
App.js
const App = () => {
return (
<div className="app__container">
<Header />
<Nav />
<BrowserRouter>
<Routes>
{/* This two component get warning */}
<Categories />
<Details />
</Routes>
</BrowserRouter>
</div>
)
}
Categories.js
const Categories = () => {
return (
<>
<Route path="/" element={<Navigate replace to="/movie" />} />
<Route path="/movie" element={<Movies /> } />
<Route path="/tv" element={<Tv />}/>
<Route path="/person" element={<People />} />
<Route path="/search" element={<Search />} />
</>
)
}
Details.js
const Details = () => {
return (
<>
<Route path="/movie/:tmdbID" element={<MovieDetail />}/>
<Route path="/tv/:tmdbID" element={<TvDetail />}/>
<Route path="/person/:tmdbID" element={<PersonDetail/>}/>
</>
)
}
As you can see, it seems like <Routes>
won’t take just Categories
and Details
as a children.
I’m doing it because I want the Route
to be placed in different folder/file. Which in Categories.js
and Details.js
So, how can I manage this?
2
Answers
The error is accurate.
Categories
andDetails
are not<Route>
values, the are their own component values. The fact that they have<Route>
elements within their JSX does not make them<Route>
elements.If this is a new project, you might consider going with React-Router newer createBrowserRouter() construct where you define routes as data (an array of JS objects) rather than as JSX elements. Then you could define Categories and Details as data and reference that data anywhere in your code.
Use of
createBrowserRouter()
is the recommended way forward for people using React-Router.The
Routes
component can only haveRoute
orReact.Fragment
as valid children. NeitherCategories
orDetails
can be rendered directly byRoutes
like this.Render all the routes together as in the following:
This organization uses nested routes.
Or if you are trying to code-split you can split the routes logically by routes/sub-routes where components render descendent routes. In order for descendent routes to work the parent route path appends a wildcard matcher, or splat,
"*"
to its path and the routed component renders anotherRoutes
and set ofRoute
components.