Conditional Rendering in React
Learn how to display different content based on conditions using simple React examples and interactive output.
1. What Is Conditional Rendering?
Conditional rendering means displaying different content depending on whether a condition is true or false.
For example, a website can display a welcome message when a user is logged in and a login message when the user is logged out.
function Welcome() {
const isLoggedIn = true;
if (isLoggedIn) {
return <h2>Welcome to CIIT!</h2>;
}
return <h2>Please log in.</h2>;
}
export default Welcome;
Explanation:
If isLoggedIn is true, React displays
"Welcome to CIIT!". If it is false, React displays
"Please log in.".
The condition is true, so the welcome message appears.
2. Conditional Rendering Using if-else
We can use a normal JavaScript if-else
statement to decide which JSX content a component should return.
function Result() {
const marks = 75;
if (marks >= 40) {
return <h3>You passed!</h3>;
} else {
return <h3>You failed.</h3>;
}
}
export default Result;
3. Conditional Rendering Using a Variable
We can store JSX in a variable and choose what to display before returning the component.
function CourseMessage() {
const hasCourse = true;
let message;
if (hasCourse) {
message = <h3>React Course Selected</h3>;
} else {
message = <h3>Please select a course</h3>;
}
return <div>{message}</div>;
}
export default CourseMessage;
Here, the message variable stores the JSX that
should be displayed. React renders the variable inside
curly braces.
4. Conditional Rendering with the Ternary Operator
The ternary operator is a shorter way to choose between two values based on a condition.
Its syntax is:
condition ? valueIfTrue : valueIfFalse
function LoginMessage() {
const isLoggedIn = false;
return (
<div>
{isLoggedIn
? <h3>Welcome Back!</h3>
: <h3>Please Log In</h3>}
</div>
);
}
export default LoginMessage;
5. Conditional Rendering Using Logical AND (&&)
The logical AND operator (&&) is useful
when you want to display something only if a condition is true.
function Notification() {
const hasMessage = true;
return (
<div>
{hasMessage && <p>You have a new message!</p>}
</div>
);
}
export default Notification;
hasMessage is true.
If the condition is false, the message is not rendered.
&& with numbers.
For example, count && ... can render
0 when count is zero. Use
count > 0 && ... if you only want
to display content when the count is positive.
6. Multiple Conditions
We can check more than one condition using
if-else if-else. This is useful when different
results are required for different values.
function Grade() {
const marks = 85;
let grade;
if (marks >= 90) {
grade = "A";
} else if (marks >= 75) {
grade = "B";
} else if (marks >= 40) {
grade = "C";
} else {
grade = "Fail";
}
return <h3>Grade: {grade}</h3>;
}
export default Grade;
- 90–100: Grade A
- 75–89: Grade B
- 40–74: Grade C
- Below 40: Fail
7. Difference Between Conditional Rendering Methods
| Method | Best Used For | Example |
|---|---|---|
if-else |
Complex conditions and multiple statements. | Checking a student's result. |
| Variable | Storing JSX before returning it. | Choosing a message. |
Ternary ?: |
Choosing between two outputs. | Login or logout message. |
Logical && |
Showing content only when true. | Displaying a notification. |
8. Practice Questions
- What is conditional rendering in React?
- How can you use an
if-elsestatement to render JSX? - What is the ternary operator?
- How does the logical AND operator work in JSX?
- What is the difference between a ternary operator and logical AND?
- Create a component that displays a message based on login status.
- Create a result checker that displays Pass or Fail.
- Create a notification that appears only when a checkbox is selected.
Summary
Conditional rendering allows React to display different content
based on conditions. Use if-else for complex logic,
variables to store JSX, the ternary operator to choose between
two outputs, and && to display content only
when a condition is true.