Force A Component To Only Have One Child
A component can normally have an arbitrary number of elements nested directly inside it. React's Children.only
function can be used to force it to a single direct child.
The React docs describe the behavior of Children.only
as such, "Returns the only child in children. Throws otherwise.".
If you modify the return
in App
to contain the following JSX
then an error will be thrown (React.Children.only expected to receive a single React element child
).
The Provider
component in react-redux
is an example of where this is used.
Last updated