236

Being a beginner to React world, I want to understand in depth what happens when I use {this.props.children} and what are the situations to use the same. What is the relevance of it in below code snippet?

render() {
  if (this.props.appLoaded) {
    return (
      <div>
        <Header
          appName={this.props.appName}
          currentUser={this.props.currentUser}
        />
        {this.props.children}
      </div>
    );
  }
}
VLAZ
  • 26,331
  • 9
  • 49
  • 67
nim007
  • 2,958
  • 3
  • 16
  • 28

8 Answers8

341

What even is ‘children’?

The React docs say that you can use props.children on components that represent ‘generic boxes’ and that don’t know their children ahead of time. For me, that didn’t really clear things up. I’m sure for some, that definition makes perfect sense but it didn’t for me.

My simple explanation of what this.props.children does is that it is used to display whatever you include between the opening and closing tags when invoking a component.

A simple example:

Here’s an example of a stateless function that is used to create a component. Again, since this is a function, there is no this keyword so just use props.children

const Picture = (props) => {
  return (
    <div>
      <img src={props.src}/>
      {props.children}
    </div>
  )
}

This component contains an <img> that is receiving some props and then it is displaying {props.children}.

Whenever this component is invoked, {props.children} will also be displayed and this is just a reference to what is between the opening and closing tags of the component.

//App.js
render () {
  return (
    <div className='container'>
      <Picture key={picture.id} src={picture.src}>
          //what is placed here is passed as props.children  
      </Picture>
    </div>
  )
}

Instead of invoking the component with a self-closing tag <Picture /> if you invoke it with full opening and closing tags <Picture> </Picture> you can then place more code between it.

This de-couples the <Picture> component from its content and makes it more reusable.

Reference: A quick intro to React’s props.children

Yogesh Umesh Vaity
  • 41,009
  • 21
  • 145
  • 105
Soroush Chehresa
  • 5,490
  • 1
  • 14
  • 29
  • If reusability is not concerned for the child nodes, is there any performance difference when calling the {props.children} from child component than using it inside the child component? – nim007 Apr 07 '18 at 11:53
  • 1
    @Nimmy when using {props.children} we don't have performance issue, just need passed component as props. and when we can using children inside the child component don't need to use {props.children} – Soroush Chehresa Apr 07 '18 at 12:17
  • A really good read: https://daveceddia.com/pluggable-slots-in-react-components/?utm_campaign=0731slots – Morris Jul 31 '18 at 19:20
  • 2
    Example is from https://codeburst.io/a-quick-intro-to-reacts-props-children-cb3d2fce4891 – Alpit Anand Feb 03 '19 at 10:44
  • 2
    @AlpitAnand You can see reference at the end of the answer :| – Soroush Chehresa Feb 03 '19 at 10:48
  • ok, tooo bad, was reading it side by side, didnt get to the end though :p – Alpit Anand Feb 03 '19 at 11:11
  • @AlpitAnand Usually, place reference to the main answer or article is the end of the answer. It's a routine when you need just answer to resolve your problem. Thanks for your attention ;) – Soroush Chehresa Feb 03 '19 at 11:26
  • 1
    I like how you used quotes for your text. – dawn Sep 17 '20 at 16:57
  • I think invoking a component means using a component instance.. – nCardot Oct 03 '21 at 04:18
  • 1
    This is explained well @SoroushChehresa, thank you. Documentation should use "simple explanations" more often... – Corné Oct 07 '21 at 07:35
  • Further details on this link of same example https://codeburst.io/a-quick-intro-to-reacts-props-children-cb3d2fce4891 – Aditya kumar Dec 10 '21 at 08:38
48

props.children represents the content between the opening and the closing tags when invoking/rendering a component:

const Foo = props => (
  <div>
    <p>I'm {Foo.name}</p>
    <p>abc is: {props.abc}</p>

    <p>I have {props.children.length} children.</p>
    <p>They are: {props.children}.</p>
    <p>{Array.isArray(props.children) ? 'My kids are an array.' : ''}</p>
  </div>
);

const Baz = () => <span>{Baz.name} and</span>;
const Bar = () => <span> {Bar.name}</span>;

invoke/call/render Foo:

<Foo abc={123}>
  <Baz />
  <Bar />
</Foo>

props and props.children

zeljko_a
  • 3,725
  • 1
  • 22
  • 23
41

I assume you're seeing this in a React component's render method, like this (edit: your edited question does indeed show that):

class Example extends React.Component {
  render() {
    return <div>
      <div>Children ({this.props.children.length}):</div>
      {this.props.children}
    </div>;
  }
}

class Widget extends React.Component {
  render() {
    return <div>
      <div>First <code>Example</code>:</div>
      <Example>
        <div>1</div>
        <div>2</div>
        <div>3</div>
      </Example>
      <div>Second <code>Example</code> with different children:</div>
      <Example>
        <div>A</div>
        <div>B</div>
      </Example>
    </div>;
  }
}

ReactDOM.render(
  <Widget/>,
  document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

children is a special property of React components which contains any child elements defined within the component, e.g. the divs inside Example above. {this.props.children} includes those children in the rendered result.

...what are the situations to use the same

You'd do it when you want to include the child elements in the rendered output directly, unchanged; and not if you didn't.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • If reusability is not concerned for the child nodes, is there any performance difference when calling the {props.children} from child component than using it inside the child component? – nim007 Apr 07 '18 at 11:55
  • @Nimmy: I'm sorry, I don't know what you mean by *"...than using it inside the child component". – T.J. Crowder Apr 07 '18 at 12:16
  • 1
    I meant "calling {this.props.children} in the child component than writing the nodes directly in the respective child component". – nim007 Apr 07 '18 at 12:47
  • @Nimmy: I'm sorry, I'm still not getting it. Can you try to say what you'd do the other way with the code above? – T.J. Crowder Apr 07 '18 at 12:53
  • 3
    @Nimmy I am assuming you are thinking why should I put {this.props.children} instead I can write it down, I know it. If that is the case please do that and have a slight advantage in performance. But your component will be static, it cant be reused with set of different children in a different place in your codebase. – Subin Sebastian Apr 07 '18 at 13:39
  • 3
    @ssk: Ah! Nice one. Nimmy - Yes, you could write the children directly in your `render`, but they it would be the same children in every instance. By accepting child elements (where appropriate), each instance of your component can have different content. I've updated the example in the answer. – T.J. Crowder Apr 07 '18 at 13:56
  • Where should we use {this.props.children} and where react's render props ? What is difference between them ? – SJxD Oct 27 '20 at 23:15
10

This is answered, but wanted to add an optimization example. You can decompose inline to only get specific properties instead of the entire props object (which means you don't have to keep writing props).

const MyView = ({title, children}) => {
  return (
    <>
      <h1>{title}</h1>
      {children}
    </>
  );
}

Then you'd use it like so:

import { MyView } from './MyView';

const MyParentView = () => {
  return (
    <MyView title="Hello">
      <h2>World</h2>
    </MyView>
  );
}

The end result would be HTML which renders out to something like this:

<div>
  <h1>Hello</h1>
  <h2>World</h2>
</div>
BTR
  • 4,880
  • 4
  • 24
  • 21
2

Children is a reserved name, and the value of this special children prop will always be the content between the opening and closing tags of your custom component. So in this case, this content between the opening and closing tag, that is what will be available on props children inside of that component.

Homa
  • 21
  • 2
2

For a children, there is a parent which is openin/closing tags as it can be seen in React's official example, which is FancyBorder.

h1 and p are the children of FancyBorder. You can examine it in practice on the CodePen link.

enter image description here

0

The important thing to note here is that children are a special prop that is used to pass the data from the parent component to the children component but this data must be enclosed within the parent’s opening and closing tag. This is used mostly in some wrapper component by which we have to pass the data onto the next component and also the data which we pass its static data ( in most cases ) because for dynamic data there is another way to pass props to the component.

Here is the example

Kowsigan Atsayam
  • 446
  • 1
  • 9
  • 20
0

In React, the props.children property refers to the child elements that are passed to a React component.

For example, in the following code:

<MyComponent>
  <div>Child 1</div>
  <div>Child 2</div>
</MyComponent>

The MyComponent component has two children: a div element containing the text "Child 1" and a div element containing the text "Child 2". These children can be accessed and rendered by the MyComponent component using the props.children property.

For example, the MyComponent component could render its children like this:

class MyComponent extends React.Component {
  render() {
    return (
      <div>
        {this.props.children}
      </div>
    );
  }
}

This would render the two div elements as children of the MyComponent component.

netanel
  • 36
  • 2