Understanding props.children in React with an example

Naveen Mathews Renji
3 min readMar 21, 2023

--

One great feature of React is the ability to reuse components in different parts of your application. This means you can create a component once and use it in many different places, customized based on the props you pass to it.

The special prop called props.children allows you to pass child components or elements to a parent component. This means that you can create flexible, reusable components that can be customized with any content you want. So, you can take a basic component and fill it with whatever content you like, making it unique and tailored to your specific needs.

react JS children props

What is props.children?

It is a feature allows you to pass any content you want into a component, whether it’s a simple text string or a complex React component. With props.children, you can create components that can be used in many different parts of your application, and can be customized with different content each time they are used. This makes your code more modular and easier to maintain in the long run. Overall, props.children is a powerful tool that can help you create more versatile and reusable components in your React applications.How to use props.children

How to use props.children

Using props.children is easy. In your parent component, simply add {props.children} where you want the child content to appear. Here’s an example:

function ParentComponent(props) {
return (
<div>
<h1>{props.title}</h1>
<div>{props.children}</div>
</div>
);
}

In this example, the ParentComponent component accepts a title prop and includes a div element that displays the title prop and another div element that displays props.children.

Now, when we render this component with some child elements like this:

<ParentComponent title="Parent Title">
<p>Child Element 1</p>
<p>Child Element 2</p>
</ParentComponent>

The resulting HTML output would be:

<div>
<h1>Parent Title</h1>
<div>
<p>Child Element 1</p>
<p>Child Element 2</p>
</div>
</div>

In this example, the child elements p with content “Child Element 1” and “Child Element 2” are passed to the ParentComponent using props.children. The child elements are then rendered within the div element with the props.children property.

Passing components as children

You can also pass React components as children. This is useful when you want to create a reusable component that can be customized with any other component.

function Card(props) {
return (
<div className="card">
<div className="card-header">{props.title}</div>
<div className="card-body">{props.children}</div>
</div>
);
}

function App() {
return (
<Card title="My Card">
<p>Some content here</p>
<button>Click me</button>
</Card>
);
}

In this example, we’re creating a Card component that accepts a title prop and any child components as props.children. The child components are then rendered within the card-body element of the Card.

When we render the Card component in our App component, we’re passing in a p element and a button element as children.

Conditionally rendering children

You can also conditionally render children based on the parent component’s state or props. This is useful when you want to create a component that can be customized based on user input or other factors.

Here’s an example:

function ToggleButton(props) {
const [showContent, setShowContent] = useState(false);
const handleClick = () => {
setShowContent(!showContent);
};
return (
<div>
<button onClick={handleClick}>{props.buttonText}</button>
{showContent && props.children}
</div>
);
}
function App() {
return (
<ToggleButton buttonText="Show Content">
<p>Some hidden content here</p>
</ToggleButton>
);
}

In this example, we’re creating a ToggleButton component that accepts a buttonText prop and any child components as props.children. The ToggleButton component also has a state variable showContent that determines whether to display the child components.

When we render the ToggleButton component in our App component, we’re passing in a p element as a child. When the user clicks the button, the handleClick function is called, which toggles the value of showContent. If showContent is true, the child components are displayed. If showContent is false, the child components are hidden.

So we’ve covered the basics of props.children in React. props.children is a powerful feature that allows you to create flexible, reusable components that can be customized with any content you want. Whether you’re passing in simple HTML elements or complex React components, props.children gives you the flexibility to create components that can be used in many different parts of your application.

--

--