Rendering JSX Elements
Learn how JSX elements are created, returned from React components, rendered conditionally, and updated using dynamic JavaScript values.
1. What Is Rendering?
Rendering means displaying the user interface produced by a React component. A component returns JSX that describes what should appear on the screen.
function App() {
return (
<div>
<h1>Welcome to CIIT Training Institute</h1>
<p>Learn React step by step.</p>
</div>
);
}
export default App;
2. Rendering Flow Diagram
Understand the basic flow from a component to the displayed user interface.
Conceptual flow: a component returns JSX, and React renders the corresponding user interface.
3. Rendering Multiple Elements
A component can return multiple JSX elements when they are wrapped inside one parent element or a React Fragment.
function App() {
return (
<section>
<h2>Our Training Programs</h2>
<p>Java Full Stack</p>
<p>.NET Full Stack</p>
<p>Python Full Stack</p>
</section>
);
}
export default App;
Expected Browser Output
Our Training Programs
Java Full Stack
.NET Full Stack
Python Full Stack
4. Rendering Dynamic Values
JavaScript variables and expressions can be placed inside JSX curly braces to display dynamic information.
function App() {
const studentName = "Priya";
const courseName = "React JS";
const totalLessons = 25;
return (
<div>
<h2>Student Dashboard</h2>
<p>Student: {studentName}</p>
<p>Course: {courseName}</p>
<p>Total Lessons: {totalLessons}</p>
</div>
);
}
export default App;
Expected Browser Output
Student Dashboard
Student: Priya
Course: React JS
Total Lessons: 25
5. Conditional Rendering
Conditional rendering means displaying different content based on a condition. A ternary expression can choose between two values.
function App() {
const isLoggedIn = true;
return (
<div>
<h2>Student Portal</h2>
<p>
{isLoggedIn
? "Welcome back, Student!"
: "Please log in to continue."}
</p>
</div>
);
}
export default App;
isLoggedIn is true, the welcome message
is displayed. When it is false, the login message appears.
6. Rendering Lists Using map()
The JavaScript map() method transforms array
items into JSX elements. React uses the resulting elements
to display a list.
function App() {
const courses = [
"Java Full Stack",
".NET Full Stack",
"Python Full Stack"
];
return (
<div>
<h2>Available Courses</h2>
<ul>
{courses.map((course) => (
<li key={course}>{course}</li>
))}
</ul>
</div>
);
}
export default App;
Expected Browser Output
Available Courses
- Java Full Stack
- .NET Full Stack
- Python Full Stack
7. Rendering Empty Values
React does not render null,
undefined, or Boolean values as ordinary
text when they are used as children.
function App() {
const message = null;
const isVisible = false;
return (
<div>
<p>Start Learning</p>
{message}
{isVisible}
<p>Continue Learning</p>
</div>
);
}
export default App;
Expected Browser Output
Start Learning
Continue Learning
8. Common Mistakes
1. Returning multiple sibling elements directly
Incorrect:
return (
<h2>React Tutorial</h2>
<p>Learn JSX</p>
);
Correct:
return (
<div>
<h2>React Tutorial</h2>
<p>Learn JSX</p>
</div>
);
2. Forgetting the key property in lists
Avoid:
courses.map(course => <li>{course}</li>)
Use:
courses.map(course => <li key={course}>{course}</li>)
3. Expecting false to appear as text
{false} does not display the word
"false" as a text node. Convert it to a string if
you want to display that word.
9. Practice Questions
- Create a component that renders a heading and three paragraphs.
- Display a student's name, course, and score using variables.
- Show a different message depending on a Boolean value.
- Render a list of five course names using
map()and stable keys. - Display a message only when a condition is true.
10. Summary
- Components return JSX to describe the user interface.
- Multiple sibling JSX elements need a common parent or Fragment.
- Curly braces allow JavaScript expressions inside JSX.
- Conditional expressions can display different content.
- The
map()method can render arrays as lists. - List items should have stable, unique keys among siblings.