New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[v6] [Feature] Explicitly warn/error against custom children in <Routes>
#8190
Comments
I'd be open to adding an invariant to the code for this. You're welcome to submit a PR. FWIW I wrote up a doc on route composition for anyone who may be interested: https://gist.github.com/mjackson/d54b40a094277b7afdd6b81f51a0393f It explains our rationale for making this decision as well as an alternative pattern for doing whatever you were doing before with your custom Route-like element. |
It should probably just be invariant(
element.type === Route,
'...'
); |
Thanks, @mjackson! Opened a PR here #8238
Imo, it actually might be really helpful to include a link to that write up in the error message. I didn't include it in the PR. However, I still think a link to the rational might help guide people into better patterns.
You're right 😅 all tests pass locally. I misread how |
This was released in 6.0.1 |
It's clear from #8111 (comment) and #8092 (comment) that support for custom routes (or rendering anything other than a
<Route/>
orReact.Fragment
component as a child of<Routes>
) is discouraged when someone asks about it.Problem
I can see some people getting confused as to why this kind of thing wouldn't like most react components do.
toyExample.js
Solution
In the example above,
<Routes>
receives an array of objects on the children propIn the
createRoutesFromChildren
function, we can modify it to also check for ifelement.type !== Route
and warn in development mode.Here's a rough version of what could be done:
Additional Notes
Potentially just a warning could be issued.
We could also use
warning
instead ofinvariant
but I noticed that the<Route>
function itself uses invariant if it ever actually gets ran as a function or component (rather than used in any context where it would be statically analyzed).The above solution doesn't immediately work locally.
Tried inserting the above solution and running tests locally, but ended up with a lot of red. Not sure exactly what would need to be done to implement this.
Potentially a
Symbol
orstring
constant could be attached as a key on theRoutes
function, and that could be checked? Not sure if that's ideal or desired but just ideas.The text was updated successfully, but these errors were encountered: