3

There are other similar questions that have been "answered", but the answers aren't complete and they don't solve the real problem.

This is really a two-parter with a sub-question.

    1. What are the general situations you would want to use this.props.children?

    2. What are some specific situations with examples?

I have read the docs, please don't only quote the docs.

Motivation for this quesiton:

The reason I for this question is that at the time of this question, the docs give a very very general example of how to use it and the methods to use when doing this, but the example don't give us true context. It isn't usable IRL. The example could be literally reused all over an app without rhyme, reason, or regards to sanity. Is that really ok?

I have been told you will use this.props.children, "when you don't know what children will be passed." But technically you never really know what changes may come down the line that require more or different children. So this is pretty ambiguous.

Also I found an interesting article by Jake Trent: http://jaketrent.com/post/send-props-to-children-react/

The article shows how you can add/remove props being passed to these "unknown children" based on the type of the component, which is neat(and possibly a good idea when you build a third party component).

My big takeaway so far is that you are giving power over the children to the parent, when you specify this.props.children Is this a good reason or just a byproduct? (example: proptypes, actual typechecking ... ) but,

   2a. What other things would/could/should you do here?
   2b. What things should you not do?

Examples please!

Urasquirrel
  • 1,461
  • 1
  • 18
  • 34
  • I don't really understand what's the problem. `this.props.children` is the simple way to insert one component to another. As soon as your app isn't single giant component you need them. – Boris Zagoruiko Oct 26 '16 at 17:58
  • Well my general understanding from everyone I speak with and everywhere I read, is that you should not just use them everywhere just for fun. I am starting to understand that they are wrappers, but when to use them and when to never use them is still confusing. – Urasquirrel Oct 26 '16 at 19:29

1 Answers1

3

I like to think of components that make use of props.children as general wrappers, like most other HTML elements.

Think about the fieldset element.

<form action="/post">
   <input type="text" name="myValue" placeholder="Some Data" />
   <input type="submit" value="Submit!" />
</form>

What a pretty form we have made! But I want it to look...different. I want it to kind of look more visually like a form. Like I want some borders around it, maybe a little label in the top to describe it...Ahh I'll wrap it in a fieldset!

<fieldset>
   <legend>A Pretty Form</legend>
   <form action="/post">
      <input type="text" name="myValue" placeholder="Some Data" />
      <input type="submit" value="Submit!" />
   </form>
</fieldset>

Now it looks just how I want! Sprinkled in a little legend too, to make this example...legendary.

The point is though, fieldset provides a reusable component with certain attributes that can be used to group HTML content. It's child elements are absolutely irrelevant to it's functionality.

Could we use the same principle in React? Yes. And it can be much more powerful!

Let's say we want to have a special ReactFieldset component, based on an HTML fieldset, that has it's own functionality. I'm not going to code that all out, because who has the time? But imagine you made a ReactFieldset component with a bunch of it's own functionality: it can collapse, it can move, it can do all kinds of things! And it can also hold any kind of content.

Well, then all you need to do in it's render function is:

render() {
    return <fieldset { ...superFunctionalityControllingProps }>
    { this.props.children }
    </fieldset>
}

Tada! You now have a reusable component that has it's own functionality, and can hold any children.

So the point is: this.props.children can be used when the children aren't really that important at all! The emphasis can actually just be on the component itself, whereas the children are arbitrary components it just happens to house. They needn't even be functionality related at all.

This is used a lot in React Native. Many different components have their own functionality, but can house any children, because they are just "wrapper" building blocks.

aaronofleonard
  • 2,546
  • 17
  • 23
  • I have noticed some of these get very specific and expect some of the children to be specific. Would you consider this a bad idea? I supposed another 3rd part to this question is, "What should you never do with this.props.children?" – Urasquirrel Oct 26 '16 at 19:35
  • I can't say I'm experienced enough to know with certainty, but I imagine that in many cases having a parent element tightly-coupled to certain children would be a bad idea. It would severely limit the reusability of the components if one always depended on the other. In the case parent needing certain children, it's probably better to directly render those components themselves by importing them, rather than relying on `props.children`, because you have no way of *really* controlling what children you get. – aaronofleonard Oct 26 '16 at 19:39
  • Actually I think that is what has ended up happening with some I have seen. From what I can surmise the consensus is that this is useful for creating general wrappers, and making them do too much or giving them too specific of functionality is a bad idea, and limits the scope of their usage. KISS(keep it stupid simple) seems to work well here. – Urasquirrel Oct 26 '16 at 19:54