DEV Community

Usama
Usama

Posted on

🌱 Understanding `props.children` in React — The Secret to Reusable Components

If you’ve been learning React, you’ve probably passed props to components like this:

<MyButton text="Click me!" />
Enter fullscreen mode Exit fullscreen mode

But what if you want to pass JSX elements or even other components between your component’s opening and closing tags?
That’s where props.children comes in! 🌟


💡 What Is props.children?

In React, every component automatically receives a special prop called children.
It contains whatever is written between the opening and closing tags of that component.

For example:

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

export default function App() {
  return (
    <Card>
      <h2>Hello React!</h2>
      <p>This content is inside the Card component.</p>
    </Card>
  );
}
Enter fullscreen mode Exit fullscreen mode

Output:

<div class="card">
  <h2>Hello React!</h2>
  <p>This content is inside the Card component.</p>
</div>
Enter fullscreen mode Exit fullscreen mode

🧱 Why It’s Useful

props.children makes components flexible and reusable.

Let’s say you’re creating a reusable Modal or Layout component — you don’t need to hardcode what’s inside. You can let the parent decide!

function Modal({ children }) {
  return (
    <div className="modal">
      <div className="content">{children}</div>
    </div>
  );
}

export default function App() {
  return (
    <Modal>
      <h1>Welcome!</h1>
      <p>This is a reusable modal window.</p>
    </Modal>
  );
}
Enter fullscreen mode Exit fullscreen mode

Now you can reuse Modal anywhere with different content!


🪄 Bonus Tip: Combine props.children with Other Props

You can mix children with other props for even more control:

function Card({ title, children }) {
  return (
    <div className="card">
      <h3>{title}</h3>
      <div>{children}</div>
    </div>
  );
}

export default function App() {
  return (
    <Card title="React Magic ✨">
      <p>props.children makes components super dynamic!</p>
    </Card>
  );
}
Enter fullscreen mode Exit fullscreen mode

🧠 Key Takeaways

  • props.children allows you to pass JSX between component tags.
  • It helps you build flexible, reusable components.
  • Common use cases: modals, cards, layouts, wrappers, tooltips, etc.

🚀 Final Thoughts

Understanding props.children is a small but powerful step in mastering React’s component system. Once you get comfortable with it, you’ll start building cleaner, reusable, and more composable UI components — like a real pro!


💬 What about you?
How did you first use props.children in a project? Share your experience in the comments below!

Top comments (0)