React props children

⚛️ React props.children

props.children allows a component to receive and render content passed between its opening and closing tags.

This is useful when you want your component to act as a wrapper, layout, or dynamic container.


🔹 Basic Example


 

export default App;

Output:

Card
└── <h2>Hello React</h2>
└── <p>This is inside the Card component.</p>

🔹 Using Destructuring

✔ Cleaner and recommended syntax


🔹 Passing Multiple Children

Children can be:

  • Text

  • JSX elements

  • Components

  • Arrays

Example:


🔹 Using Children with Styling or Layout


 


🔹 Conditional Rendering with Children


 


🔹 Children as a Function (Advanced Pattern: Render Props)


 

This makes components extremely reusable.


🎯 When to Use props.children

Use it when:

ScenarioExample
Reusable layoutCards, modals, containers
Wrapper componentsNavigation bars, section blocks
Dynamic UITabs, lists, sliders
Render props patternCustom logic with children function

Summary

FeatureExplanation
props.childrenSpecial prop for nested content
SupportsJSX, components, functions, text
Useful forLayouts, wrappers, reusable components
OptionalWorks even if nothing is passed

✔ Key Takeaway:

props.children makes components flexible and reusable by allowing dynamic nested content.

You may also like...