O'Reilly logo

Mastering React by Ryan Vice, Adam Horton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Accessing a component's children

In React when we want to access the inner HTML of a component or a component that's been embedded inside of a component we can use this.props.children. This feature is very similar to Angular's Transclusion, WebComponent's Contents or Ember's Yield. To demonstrate this we are going to update the button from our previous example to be the Button component shown in the following code:

var Button = React.createClass({
   render: function() {
       return (
<button onClick={this.props.onClick}>
{this.props.children}
</button>
);
   }
});

What we have created here is Button component that can have its opening and closing tags wrapped around the HTML elements and React components that it wants to display within the button that it ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required