Well, basically - why is there a limit on <Router /> having a single child?
I tried looking at the code, but I couldn't really make sense of the why:
Thanks for a great library by the way - I just want to understand why stuff is like it is.
Because <Router> belongs at the top of the component tree, near your ReactDOM.render. And because it just returns its children when rendering, that will go straight to up to the top. So, this:
ReactDOM.render(
<Router>
<div />
<div />
</Router>
)
is basically this:
ReactDOM.render(
<div />
<div />
)
And that's not valid input for render.
So, we use React.Children.only to ensure we only have one child (That's the main use case for that function) and yell at you if you try to have more. Sorry for the yelling, but it's for everyone's benefit.
Ah that鈥檚 why! Thanks a ton for taking the time to explain it to me :-)
Also @selbekk thanks for asking it - had the same question myself & was nice to see it explained clearly here. Thanks for the clear response @timdorr!
Most helpful comment
Because
<Router>belongs at the top of the component tree, near yourReactDOM.render. And because it just returns its children when rendering, that will go straight to up to the top. So, this:is basically this:
And that's not valid input for
render.So, we use
React.Children.onlyto ensure we only have one child (That's the main use case for that function) and yell at you if you try to have more. Sorry for the yelling, but it's for everyone's benefit.