[Bug]: The element
prop of the <Route>
component should be typed as a ReactNode
, not ReactElement
#8456
Labels
element
prop of the <Route>
component should be typed as a ReactNode
, not ReactElement
#8456
What version of React Router are you using?
6.0.2
Steps to Reproduce
To reproduce: start with the "Auth Example" currently linked to in the docs, and change the
RequireAuth
component so thechildren
prop uses the more standardReactNode
type instead ofJSX.Element
(line 120). You'll see a type error appear on line 44:Example here: https://stackblitz.com/edit/github-37enwq?file=src/App.tsx
Expected Behavior
The
element
prop should accept aReactNode
, not aReactElement
. UsingReactElement
artificially limits what can be passed as anelement
when using the<Routes>
and<Route>
components.At first I thought this was an intentional decision that had some underlying technical reason. But under-the-hood the
<Routes>
and<Route>
component are implemented using theuseRoutes
hook. And that hook allows an element to be aReactNode
:But the
<Route>
component is typed differently:Even though under-the-hood the implementation is exactly the same (
<Routes>
simply callsuseRoutes
).In most cases this isn't a problem. But in the case above, where you have a component that does some gating and may pass-through
children
, it doesn't work whenchildren
are typed in the normal way.You can work-around this by changing how you type things, but that can cause a cascading effect in a project and require retyping a bunch of unrelated components. And it seems the official types always type
children
as aReactNode
, not as aJSX.Element
orReactElement
(as seen in the Auth Example).Actual Behavior
Only
ReactElement
s can be used when defining routes using the<Route>
component, artificially limiting what can be rendered there.The text was updated successfully, but these errors were encountered: