Conditional component rendering lets your app show or hide parts based on conditions. It helps make your app interactive and dynamic.
Conditional component rendering in React
function MyComponent({ isLoggedIn }) { return ( <div> {isLoggedIn ? <p>Welcome back!</p> : <button>Login</button>} </div> ); }
Use JavaScript expressions inside curly braces {} to decide what to render.
The ternary operator condition ? truePart : falsePart is common for conditional rendering.
function Greeting({ isMorning }) { return <div>{isMorning ? 'Good morning!' : 'Good evening!'}</div>; }
show is true.function ShowMessage({ show }) { return <div>{show && <p>This message is visible.</p>}</div>; }
switch to render different components based on status.function Status({ status }) { switch (status) { case 'loading': return <p>Loading...</p>; case 'error': return <p>Error occurred.</p>; default: return <p>Ready.</p>; } }
This component shows a login button if the user is not logged in. When clicked, it changes state to logged in and shows a welcome message with a logout button. Clicking logout switches back.
Buttons have aria-label for accessibility.
import React, { useState } from 'react'; function LoginControl() { const [isLoggedIn, setIsLoggedIn] = useState(false); function handleLogin() { setIsLoggedIn(true); } function handleLogout() { setIsLoggedIn(false); } return ( <div> {isLoggedIn ? ( <> <p>Welcome back!</p> <button onClick={handleLogout} aria-label="Logout button">Logout</button> </> ) : ( <button onClick={handleLogin} aria-label="Login button">Login</button> )} </div> ); } export default LoginControl;
Always use semantic HTML elements like <button> for clickable items.
Use aria-label to help screen readers describe buttons clearly.
Keep conditions simple to avoid confusing code.
Conditional rendering lets you show or hide parts of your UI based on data or state.
Use JavaScript expressions inside JSX with {} to decide what to render.
Common patterns include ternary operators, logical AND, and switch statements.
Practice
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 DQuick Check:
Conditional rendering = Show/hide UI [OK]
- Confusing conditional rendering with styling changes
- Thinking it automatically fetches data
- Believing it creates components without JSX
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 iscondition ? exprIfTrue : exprIfFalse, which matches{condition ? <ComponentA /> : <ComponentB />}.Final Answer:
{condition ? <ComponentA /> : <ComponentB />} -> Option BQuick Check:
Ternary in JSX = condition ? true : false [OK]
- Using if-else statements directly inside JSX
- Writing invalid JavaScript syntax inside JSX
- Confusing logical AND with ternary operator
isLoggedIn is false?
function Greeting({ isLoggedIn }) {
return (
<div>
{isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign up.</h1>}
</div>
);
}Solution
Step 1: Check the value of isLoggedIn
The value is false, so the condition in the ternary operator is false.Step 2: Evaluate the ternary operator
Since condition is false, the expression after the colon is rendered: <h1>Please sign up.</h1>.Final Answer:
<h1>Please sign up.</h1> -> Option CQuick Check:
isLoggedIn false = show sign up message [OK]
- Assuming true branch renders when condition is false
- Thinking nothing renders on false
- Confusing syntax with runtime error
function Message({ show }) {
return (
<div>
{show && <p>Hello World!</p> else <p>No message</p>}
</div>
);
}Solution
Step 1: Review JSX expression syntax
JSX expressions inside curly braces must be valid JavaScript expressions; 'else' cannot be used inside them.Step 2: Identify invalid use of else
The code uses 'else' after a logical AND expression, which is not valid JavaScript syntax.Final Answer:
Using else inside JSX expression is invalid -> Option AQuick Check:
JSX expressions cannot contain else keyword [OK]
- Trying to use else with logical AND
- Forgetting ternary operator syntax
- Assuming JSX can contain statements like else
const notifications = [];
function NotificationList() {
return (
<div>
{/* Choose the correct conditional rendering here */}
{notifications.length > 0 ? (
notifications.map((note, index) => <p key={index}>{note}</p>)
) : (
<p>No notifications</p>
)}
</div>
);
}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 AQuick Check:
Ternary handles both cases in JSX [OK]
- Using if statements directly inside JSX
- Using logical AND without else case
- Rendering map on empty array without check
Ready to prove it?
Pass the test and this pattern moves unknown
