JSX Children
Learn how to place text, elements, expressions, and reusable components inside JSX parent elements.
1. What Are JSX Children?
JSX children are the content placed between the opening and closing tags of a JSX element. Children can be text, other JSX elements, JavaScript expressions, or components.
<div>Hello React</div>
The text Hello React is the child of the
div element.
function App() {
return (
<div>
<h2>Welcome to CIIT Training Institute</h2>
<p>Learn React with practical examples.</p>
</div>
);
}
export default App;
2. JSX Children Structure
The parent element contains child elements. Those children can also contain their own children.
<div>
<h2>
Heading text
<p>
Paragraph text
function App() {
return (
<div>
<h2>React Tutorial</h2>
<p>Understanding JSX children.</p>
</div>
);
}
export default App;
3. Text as Children
Plain text can be placed directly between the opening and closing tags of an element.
function App() {
return (
<div>
<h2>Our Courses</h2>
<p>Java Full Stack</p>
<p>.NET Full Stack</p>
<p>Python Full Stack</p>
</div>
);
}
export default App;
Expected Browser Output
Our Courses
Java Full Stack
.NET Full Stack
Python Full Stack
4. JSX Elements as Children
A parent element can contain multiple nested JSX elements. This structure helps organize a page into meaningful sections.
function App() {
return (
<div className="course-card">
<h2>React Full Stack</h2>
<p>Learn component-based development.</p>
<ul>
<li>JSX</li>
<li>Components</li>
<li>Props</li>
</ul>
</div>
);
}
export default App;
5. JavaScript Expressions as Children
Curly braces allow variables and expressions to become children of JSX elements.
function App() {
const studentName = "Rahul";
const completedLessons = 8;
const totalLessons = 12;
return (
<div>
<h2>Student Progress</h2>
<p>Student: {studentName}</p>
<p>
Lessons completed:
{completedLessons} / {totalLessons}
</p>
<p>
Remaining lessons: {totalLessons - completedLessons}
</p>
</div>
);
}
export default App;
Expected Browser Output
Student Progress
Student: Rahul
Lessons completed: 8 / 12
Remaining lessons: 4
6. Components as Children
One React component can be placed inside another component. This helps you build reusable and well-organized user interfaces.
function CourseDetails() {
return (
<div>
<h3>React Full Stack</h3>
<p>Learn React step by step.</p>
</div>
);
}
function App() {
return (
<main>
<h2>Available Training</h2>
<CourseDetails />
</main>
);
}
export default App;
CourseDetails is rendered inside the
App component. Its returned JSX becomes part
of the overall user interface.
7. Passing Children to a Component
When content is placed between a component's opening and
closing tags, React makes that content available through
the special children prop.
function InfoCard({ children }) {
return (
<div className="info-card">
{children}
</div>
);
}
function App() {
return (
<InfoCard>
<h2>Welcome Student</h2>
<p>Start learning React today.</p>
</InfoCard>
);
}
export default App;
children is a prop, not a special HTML tag.
It represents the content passed between the component tags.
8. Conditional Children
A condition can determine whether content appears inside a parent element.
function App() {
const hasCompletedCourse = true;
return (
<div>
<h2>Course Status</h2>
{hasCompletedCourse && (
<p>Congratulations! Course completed.</p>
)}
</div>
);
}
export default App;
When hasCompletedCourse is true, the
congratulatory message appears. When it is false, the
message is omitted.
&& with numbers.
For example, {0 && "Message"} renders
0. Use a Boolean condition when appropriate.
9. Common Mistakes
1. Forgetting a closing tag
Incorrect:
<p>Learn React
Correct:
<p>Learn React</p>
2. Returning multiple sibling elements
Wrap sibling elements inside one parent element or a React Fragment.
return (
<>
<h2>React</h2>
<p>JSX Children</p>
</>
);
3. Forgetting to render children
If a component receives children but never
uses {children} in its returned JSX,
that content will not appear.
10. Practice Questions
- Create a parent element containing a heading and three paragraphs.
- Display student information using JavaScript expressions.
- Create a reusable component that displays its children.
- Pass a heading and paragraph to an
InfoCardcomponent. - Render a message only when a Boolean condition is true.
11. Summary
- JSX children are content inside an element's tags.
- Children can be text, nested elements, expressions, or components.
- Curly braces embed JavaScript expressions.
- Components can receive nested content through the
childrenprop. - Conditional rendering can show or omit children.
- Use valid JSX syntax and close elements correctly.