Skip to content

Instantly share code, notes, and snippets.

@nikkaroraa
Last active July 10, 2017 20:00
Show Gist options
  • Save nikkaroraa/fa95c2b2456037a62b149db64472101c to your computer and use it in GitHub Desktop.
Save nikkaroraa/fa95c2b2456037a62b149db64472101c to your computer and use it in GitHub Desktop.
The significance of this.props.children

Some components don't know their children ahead of time. This is especially common for components like Sidebar or Dialog that represent generic "boxes".

We recommend that such components use the special children prop to pass children elements directly into their output:

function FancyBorder(props) {
  return (
    <div className={'FancyBorder FancyBorder-' + props.color}>
      {props.children}
    </div>
  );
}

This lets other components pass arbitrary children to them by nesting the JSX:

  function WelcomeDialog() {
    return (
      <FancyBorder color="blue">
        <h1 className="Dialog-title">
          Welcome
        </h1>
        <p className="Dialog-message">
          Thank you for visiting our spacecraft!
        </p>
      </FancyBorder>
    );
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment