📖 Scenario: You are building a simple React component that shows a greeting message. Sometimes the user is logged in, and sometimes not. You want to show a special message only when the user is logged in.
🎯 Goal: Create a React functional component that uses an if condition inside JSX to show a welcome message only if the user is logged in.
📋 What You'll Learn
Create a boolean variable called isLoggedIn with the value true.
Create a string variable called userName with the value 'Alice'.
Use an if condition inside the JSX to show Welcome, Alice! only if isLoggedIn is true.
If isLoggedIn is false, show Please log in. instead.
💡 Why This Matters
🌍 Real World
Showing different content based on user login status is very common in websites and apps.
💼 Career
Understanding conditional rendering in React is essential for building interactive user interfaces.
Progress0 / 4 steps
1
Set up user login state variables
Create a boolean variable called isLoggedIn and set it to true. Also create a string variable called userName and set it to 'Alice' inside the Greeting component.
React
Hint
Use const to create variables inside the component function.
2
Add a helper variable for the greeting message
Create a variable called message that will hold the greeting text. Initialize it as an empty string '' inside the Greeting component, below the isLoggedIn and userName variables.
React
Hint
Use let because you will change the value later.
3
Use an if condition to set the message
Use an if statement to check if isLoggedIn is true. If yes, set message to `Welcome, ${userName}!`. Otherwise, set message to 'Please log in.'.
React
Hint
Use backticks ` for the template string with ${userName}.
4
Render the message inside JSX
Inside the return statement, replace the comment with JSX that displays the message variable inside a <p> tag.
React
Hint
Use curly braces {} to insert the variable inside JSX.
Practice
(1/5)
1. Which of the following is the correct way to conditionally render a message in JSX when isLoggedIn is true?
easy
A. {if (isLoggedIn) <p>Welcome back!</p>}
B. {isLoggedIn && <p>Welcome back!</p>}
C. {isLoggedIn ? <p>Welcome back!</p>}
D. {isLoggedIn || <p>Welcome back!</p>}
Solution
Step 1: Understand JSX conditional rendering with &&
In JSX, condition && expression renders the expression only if the condition is true.
Step 2: Check each option's syntax
{isLoggedIn && <p>Welcome back!</p>} uses {isLoggedIn && <p>Welcome back!</p>}, which is correct. {if (isLoggedIn) <p>Welcome back!</p>} uses an invalid if statement inside JSX. {isLoggedIn ? <p>Welcome back!</p>} uses an incomplete ternary operator without the false branch (`: expression`), causing a syntax error. {isLoggedIn || <p>Welcome back!</p>} uses ||, which is incorrect for this purpose.
Final Answer:
{isLoggedIn && <p>Welcome back!</p>} -> Option B
Quick Check:
Use && for simple true condition rendering [OK]
Hint: Use && to show content only if true [OK]
Common Mistakes:
Trying to use if statements directly inside JSX
Using || instead of && for conditional rendering
Confusing ternary with && when only one condition is needed
2. Which JSX snippet correctly uses a ternary operator to show <p>Hello</p> if showHello is true, otherwise <p>Goodbye</p>?
easy
A. {showHello && <p>Hello</p> else <p>Goodbye</p>}
B. {showHello ? <p>Goodbye</p> : <p>Hello</p>}
C. {if (showHello) <p>Hello</p> else <p>Goodbye</p>}
D. {showHello ? <p>Hello</p> : <p>Goodbye</p>}
Solution
Step 1: Understand ternary syntax in JSX
The ternary operator syntax is condition ? expressionIfTrue : expressionIfFalse.
Step 2: Match the expressions to the condition
{showHello ? <p>Hello</p> : <p>Goodbye</p>} correctly shows Hello if showHello is true, else Goodbye. {showHello && <p>Hello</p> else <p>Goodbye</p>} uses invalid syntax with else inside JSX. {if (showHello) <p>Hello</p> else <p>Goodbye</p>} tries to use if statement inside JSX, which is invalid. {showHello ? <p>Goodbye</p> : <p>Hello</p>} reverses the messages.
Final Answer:
{showHello ? <p>Hello</p> : <p>Goodbye</p>} -> Option D
Quick Check:
Ternary = condition ? true : false [OK]
Hint: Ternary: condition ? true : false for two choices [OK]
The ternary operator requires both true and false expressions: condition ? exprIfTrue : exprIfFalse. Here, the false part is missing.
Step 2: Understand JSX requirements
Without the false expression, JSX will throw a syntax error because the ternary is incomplete.
Final Answer:
Missing else part in ternary operator -> Option C
Quick Check:
Ternary needs both ? and : parts [OK]
Hint: Ternary must have both ? and : parts [OK]
Common Mistakes:
Omitting the else (false) part in ternary
Confusing ternary with && operator
Trying to use if statements inside JSX
5. You want to render a list of user names, but only show the name if the user is active. Which JSX code correctly implements this using map and conditional rendering?
hard
A. {users.map(user => user.isActive ? <li key={user.id}>{user.name}</li> : null)}
B. {users.map(user => if (user.isActive) return <li key={user.id}>{user.name}</li> )}
C. {users.map(user => user.isActive || <li>{user.name}</li>)}
D. {users.map(user => user.isActive ? <li>{user.name}</li> : <li>Inactive</li>)}