Children Props
Learn how to pass content between the opening and closing tags of a React component and display it using the special children prop.
01 What Is the Children Prop?
The special children prop contains the content placed
between a component's opening and closing tags. That content can
be text, JSX elements or other components.
children is special in React: it represents
nested content passed between a component's tags.
Samadhan is learning React JS
02 Passing Simple Text as Children
Text placed between the component tags becomes the
children value.
function App() {
return (
<Message>
Welcome to CIIT Training Institute!
</Message>
);
}
function Message({ children }) {
return <p>{children}</p>;
}
export default App;
Welcome to CIIT Training Institute!
03 Passing JSX Elements as Children
Children can contain JSX elements, not just plain text. The receiving component can render those elements inside its layout.
function App() {
return (
<StudentCard>
<h2>Samiksha</h2>
<p>Course: React JS</p>
</StudentCard>
);
}
function StudentCard({ children }) {
return (
<div>
{children}
</div>
);
}
export default App;
Samiksha
Course: React JS
04 Using Children Inside a Card
A reusable card component can provide the common border and spacing while allowing the parent to decide what content appears.
function App() {
return (
<Card>
<h3>React JS</h3>
<p>Learn components and props.</p>
</Card>
);
}
function Card({ children }) {
return (
<div className="card">
{children}
</div>
);
}
export default App;
.card CSS class
in your stylesheet to style the container.
React JS
Learn components and props.
05 Passing Multiple Elements as Children
Multiple JSX elements can be passed inside one component. React treats the nested content as the component's children.
function App() {
return (
<Panel>
<h2>Student Details</h2>
<p>Name: Pradnya</p>
<p>Course: Full Stack Development</p>
</Panel>
);
}
function Panel({ children }) {
return (
<section>
{children}
</section>
);
}
export default App;
Student Details
Name: Pradnya
Course: Full Stack Development
06 Children Along with Other Props
A component can receive regular props such as title and children together. Regular props configure the component, while children supply the nested content.
function App() {
return (
<CourseCard title="React JS">
<p>Duration: 3 Months</p>
<p>Includes hands-on practice.</p>
</CourseCard>
);
}
function CourseCard({ title, children }) {
return (
<div>
<h2>{title}</h2>
{children}
</div>
);
}
export default App;
React JS
Duration: 3 Months
Includes hands-on practice.
07 Reusable Layout with Children
Children props are useful for reusable layouts. The layout component controls the common structure, while the parent supplies the page-specific content.
function App() {
return (
<Layout>
<h2>React Components</h2>
<p>Explore reusable UI components.</p>
</Layout>
);
}
function Layout({ children }) {
return (
<main>
<header>CIIT Training Institute</header>
<section>{children}</section>
<footer>Learn by building projects</footer>
</main>
);
}
export default App;
CIIT Training Institute
React Components
Explore reusable UI components.
Learn by building projects
08 Conditional Rendering of Children
A component can render its children only when a condition is satisfied. This is useful for optional descriptions or content.
function App() {
return (
<InfoBox showDetails={true}>
<p>Students can practice React examples.</p>
</InfoBox>
);
}
function InfoBox({ showDetails, children }) {
return (
<div>
<h3>Course Information</h3>
{showDetails ? children : <p>Details hidden.</p>}
</div>
);
}
export default App;
Course Information
Students can practice React examples.
09 Handling Optional Children
A component may be rendered without nested content. You can check whether children were provided and show fallback content.
function App() {
return (
<div>
<Message>Welcome Samadhan!</Message>
<Message />
</div>
);
}
function Message({ children }) {
return (
<div>
{children ?? <p>No message available.</p>}
</div>
);
}
export default App;
Welcome Samadhan!
No message available.
10 Complete Children Props Example
This example combines regular props and children to build a reusable student profile card.
function App() {
return (
<StudentCard name="Pradnya" course="React JS">
<p>Skills: HTML, CSS and JavaScript</p>
<p>Institute: CIIT Training Institute</p>
</StudentCard>
);
}
function StudentCard({ name, course, children }) {
return (
<article>
<h2>{name}</h2>
<p>Course: {course}</p>
{children}
</article>
);
}
export default App;
Pradnya
Course: React JS
Skills: HTML, CSS and JavaScript
Institute: CIIT Training Institute
Summary
The children prop allows a React component to receive and display content placed between its opening and closing tags. This content may be plain text, JSX elements or other components. Children can be combined with regular props to build reusable cards, layouts and conditional content. Using children effectively helps create flexible, maintainable React interfaces at CIIT Training Institute.