React wrapper components with nested children
Say you want to create a component that acts as a wrapper such that you can pass it nested child components like so:
<WrapperComponent title="I am the wrapper">
<ChildComponent body="Hello from child component" />
</WrapperComponent>
This can be achieved by using this.props.children
as in this example:
class WrapperComponent extends Component {
render() {
return (
<div className="wrapper">
<h1>{this.props.title}</h1>
{this.props.children}
</div>
);
}
}
class ChildComponent extends Component {
render() {
return (
<p>{this.props.body}</p>
);
}
}
class App extends Component {
render() {
return (
<WrapperComponent title="I am the wrapper">
<ChildComponent body="Hello from child component" />
</WrapperComponent>
);
}
}
Mounting App
will produce the following markup:
<div class="wrapper">
<h1>I am the wrapper</h1>
<p>Hello from child component</p>
</div>
Tweet