I'm in a situation where I am building a component that needs to render some child components. More specifically, I have a Map component and on it I wish to display a Legend component.
const Map = props => (
<div id="map">
{ this.props.children }
</div>
);
// Usage:
const MapWithLegend = () => (
<div id="map-view">
<Map><Legend /></Map>
</div>
);
// Usage:
const MapWithoutLegend = () => (
<div id="map-view">
<Map />
</div>
);
However, this could also be expressed with named props:
const Map = ({ legend } => (
<div id="map">{ legend }</div>
);
// Usage:
const MapWithLegend = () => (
<div id="map-view">
<Map legend={Legend} />
</div>
);
// Usage:
const MapWithoutLegend = () => (
<div id="map-view">
<Map />
</div>
);
I'm not sure which way is the best in terms of scaling and reusability. Ultimately, we will have more than just legends to put over the map, for example: scales, zoom/navigation controls, etc...
Does it make sense to use React.Children for this or should I use named props?
My intuition is that using this.props.children
frees the Map from caring about whether or not it needs to render a legend, or any other child controls, but it also creates a backdoor into the Map component, ie: passing arbitrary components as children of the Map component.
Is that a concern? Is it common to inspect this.props.children
in the parent component before rendering them? If so, how?
Also, what about styling? With named props, I can position/style the "children" since they are named and I know what they are. With React.Children, it's an array of components that would need to be inspected so they can be positioned, in which case what do I gain from using this.props.children
?
I've seen third party components using both paradigms, but I'm curious what are the scenarios in which one chooses one over the other.
Passing Props to Children in React To display them, you need to include props. children or this. props. children (for class components) in your return statement.
You can pass a component as props in React by using the built-in children prop. All elements you pass between the opening and closing tags of a component get assigned to the children prop. Copied!
Instead, to pass all React props from parent to child at once, you need to take advantage of the spread operator ( ... ). The spread operator lets you pass the entire props object to a child component as that child's props object.
The child components only receive the state data they need. The second is that complex stateful apps can be broken down into just a few, or maybe a single, stateful component. The rest of your components simply receive state from the parent as props, and render a UI from that state.
I would recommend using children
for this:
legend
as an prop, you have no idea what <Map/>
will do with that.<Legend/>
Inspecting the children sounds like you will implement a big switch-case statement to handle all edge cases. Using children
won't produce any edge cases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With