Recently, I was building a site's admin section with React. The admin section used a repeated pattern for its URL structure:
/admin/:contentType
...
For further actions, you may consider blocking this person and/or reporting abuse
There is a code change you can make for some optimization and to type less.
Looking at react-router code, github.com/ReactTraining/react-rou..., you can see they purposely avoid using
React.Children.toArray()
and useReact.Children.forEach()
instead becausetoArray()
actually returns children withkey
s prefixed. The reason they do that is to preserve your statically definedRoute
s that probably don't have a key set on them. It is a minor optimization to prevent re-mounting for differentRoute
s that have the same component.So if we wanted to preserve this functionality, and not have to inject any keys, how should we write our code?
This is how your current code transpiles:
React dev mode sees that those two
AdminRouteGroup
children are arrays and gives you a warning if they are missingkey
.Instead, you can spread the children out. It would be as if you typed JSX out statically and React dev mode won't complain. In general, don't do this. The warning is there for a reason. But you can do this if you know what you are doing and know that the children are stable and won't be reorganized or modified in a render update. See Dan Abramov's comment here:
github.com/facebook/react/issues/1....
I signed up for dev.to only so that I could like this post and send this comment. I needed to map over an array and return a set of routes which does not work because of the nested Fragment within the Switch. This is a verified workaround. Thank you greatly for taking the time to write this up!
Thank you for the kind words! I'm glad you found it useful: this problem had me completely stumped for hours.
wow, i had the trouble with nested Fragment in Switch, your answer really really helped me. Thank you so much
I'm glad it helped!
This made my day, thanks!
I'm glad this helped! I got stuck on this problem for hours.
Signed up for this website to upvote your post. A clean and easy solution.