📖 Scenario: You are building a simple React app that shows a greeting message only if the user is logged in.
🎯 Goal: Create a React component that conditionally renders a greeting message based on a login status.
📋 What You'll Learn
Create a state variable to track if the user is logged in
Create a boolean variable to represent the login status
Use conditional rendering to show a greeting message only when logged in
Render a button to toggle the login status
💡 Why This Matters
🌍 Real World
Conditional rendering is common in apps to show or hide parts of the UI based on user actions or data, like showing login status or notifications.
💼 Career
Understanding conditional rendering is essential for React developers to build dynamic and interactive user interfaces.
Progress0 / 4 steps
1
Set up the initial React component and state
Create a React functional component called Greeting. Inside it, use the useState hook to create a state variable called isLoggedIn and set its initial value to false.
React
Hint
Use const [isLoggedIn, setIsLoggedIn] = useState(false); inside the component.
2
Add a button to toggle login status
Inside the Greeting component, add a button element. Set its onClick handler to toggle the isLoggedIn state between true and false using setIsLoggedIn.
React
Hint
Use <button onClick={() => setIsLoggedIn(!isLoggedIn)}> to toggle the login state.
3
Add conditional rendering for the greeting message
Inside the Greeting component's return, add a conditional rendering that shows a <p> element with the text "Welcome back!" only if isLoggedIn is true. Keep the toggle button visible always.
React
Hint
Use {isLoggedIn && <p>Welcome back!</p>} inside the return.
4
Add accessibility and semantic improvements
Wrap the button and greeting message inside a <section> element. Add an aria-live="polite" attribute to the <p> greeting message to announce changes to screen readers.
React
Hint
Wrap the content in <section> and add aria-live="polite" to the greeting paragraph.
Practice
(1/5)
1. What does conditional rendering in React allow you to do?
easy
A. Create new components on the fly without JSX
B. Change the CSS styles of components dynamically
C. Automatically fetch data from APIs
D. Show or hide parts of the UI based on data or state
Solution
Step 1: Understand the purpose of conditional rendering
Conditional rendering controls what parts of the UI appear based on conditions like state or props.
Step 2: Differentiate from other React features
Changing styles or fetching data are different tasks; conditional rendering specifically controls visibility.
Final Answer:
Show or hide parts of the UI based on data or state -> Option D
Quick Check:
Conditional rendering = Show/hide UI [OK]
Hint: Conditional rendering controls UI visibility based on conditions [OK]
Common Mistakes:
Confusing conditional rendering with styling changes
Thinking it automatically fetches data
Believing it creates components without JSX
2. Which of the following is the correct syntax for conditional rendering using a ternary operator in React JSX?
easy
A. if (condition) <ComponentA /> else <ComponentB />
B. {condition ? <ComponentA /> : <ComponentB />}
C. {if condition then <ComponentA /> else <ComponentB />}
D. {condition && <ComponentA /> else <ComponentB />}
Solution
Step 1: Recall JSX expression syntax
JSX uses curly braces {} to embed JavaScript expressions, including ternary operators.
Step 2: Identify correct ternary operator usage
The ternary operator syntax is condition ? exprIfTrue : exprIfFalse, which matches {condition ? <ComponentA /> : <ComponentB />}.
Final Answer:
{condition ? <ComponentA /> : <ComponentB />} -> Option B
Quick Check:
Ternary in JSX = condition ? true : false [OK]
Hint: Use curly braces and ? : for ternary in JSX [OK]
Common Mistakes:
Using if-else statements directly inside JSX
Writing invalid JavaScript syntax inside JSX
Confusing logical AND with ternary operator
3. Given the code below, what will be rendered if isLoggedIn is false?
A. Use ternary operator checking notifications.length > 0
B. Use only logical AND: {notifications.length && notifications.map(...)}
C. Use if statement inside JSX to check length
D. Render notifications.map(...) without any condition
Solution
Step 1: Understand the need for else case
We want to show a message if there are no notifications, so we need both true and false cases.
Step 2: Evaluate each option
Use ternary operator checking notifications.length > 0 uses a ternary operator to handle both cases correctly. Use only logical AND: {notifications.length && notifications.map(...)} only renders when length is truthy but shows nothing if empty. Use if statement inside JSX to check length is invalid syntax inside JSX. Render notifications.map(...) without any condition always renders map, which errors if empty.
Final Answer:
Use ternary operator checking notifications.length > 0 -> Option A
Quick Check:
Ternary handles both cases in JSX [OK]
Hint: Use ternary to handle both empty and non-empty lists [OK]
Common Mistakes:
Using if statements directly inside JSX
Using logical AND without else case
Rendering map on empty array without check
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.