How to use props.children in React
Tags: react, coding, webdev, programming
Published: 2019-04-19 09:59:04

---
title: How to use props.children in React
published: true
tags: react,coding,web-development,programming
canonical_url: https://medium.com/javascript-in-plain-english/how-to-use-props-children-in-react-7d6ab5836c9d
---

![](https://cdn-images-1.medium.com/max/1024/1*dBVGJq2BDqe1RWxOSS7S5w.png)

We’ll investigate it in detail today. You can also refer to the official documentation about the same [here](https://reactjs.org/docs/composition-vs-inheritance.html).

### What are children?

The children, in React, refer to the generic box whose contents are unknown until they’re passed from the parent component.

**What does this mean?** It simply means that the component will display whatever is included in between the opening and closing tags while invoking the component. The component would usually be invoked from App component.

### Example with props.children

I’ve a basic react app set up with create-react-app. I will create another component that’ll render the image with an img tag and everything else that goes along.

![](https://cdn-images-1.medium.com/max/1024/0*sFRvaTBwoOyFH2GB)

The photo component is written as self-closing is equivalent to . If we do not have children, we can use the self-closing one. You can convert that to opening and closing one, keeping nothing in between, it works the same.

In my Photo component, I try to see what is in the props.children.

![](https://cdn-images-1.medium.com/max/1024/0*VncJGBc8uEK0jdUp)

It displays the image and … NOTHING else!

![](https://cdn-images-1.medium.com/max/1024/0*xvPnByYlILYu6lH3)

That’s because the Photo component in App has no children.

Let’s say I want to pass few more components along, but the information is not complete. All I want is to have the placeholder in the Photo for any and all my components.

Let’s have a look.

![](https://cdn-images-1.medium.com/max/1024/0*tGhR2j5nMoa8H46v)

No changes are made to the Photo component. I just passed a div in between Photo.

What else do we get now?

![](https://cdn-images-1.medium.com/max/677/0*rZ7vOvqs_QjisZJB)

You might want to assume that App will render as its html within the App, but it’s already in another component Photo. It won’t render whatever goes in between in the App. But it knows them as the children of this component as a hierarchy.

Component  
\> Child  
\> AnotherChild

All these children can be accessed as this.props.children.

### The power of children is that they can be anything.

The possible usage are:

- Grouping unknown number of similar elements into a parent element.
- You don’t know elements ahead of the time.
- The nested structure that needs a wrapper.

The performance remains same with passing props and getting them via props.children, there’s nothing to worry about performance issues.

If sending props is a possibility, avoid using props.children as it’ll be difficult to manage data passed as children as the application grows and needs changes.

If multiple components need the same children, consider assigning them to the variable in render and then pass as children, I have done the same with the above example.

### Featured React JS Courses

#### [React 16 — The Complete Guide (incl. React Router 4 & Redux)](https://click.linksynergy.com/link?id=qt/jYwyHv8A&offerid=507388.1362070&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Freact-the-complete-guide-incl-redux%2F)

4.7/5 Stars **||** 33.5 Hours of Video **||** 61,597 Students

Learn React or dive deeper into it. Learn the theory, solve assignments, practice in demo projects and build one big application which is improved throughout the course: The Burger Builder! [**Learn More.**](http://thebestdevlist.link/react-the-complete-16-guide-incl-redux)

[React 16: The Complete Course (incl. React Router 4 & Redux)](https://click.linksynergy.com/link?id=qt/jYwyHv8A&offerid=507388.1362070&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Freact-the-complete-guide-incl-redux%2F)

### Disclaimer

_This post contains affiliate links to products. We may receive a commission for purchases made through these links._

* * *