Bird
Raised Fist0
Reactframework~20 mins

Conditional component rendering in React - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Conditional Rendering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this React component?

Consider this React component that conditionally renders a message based on a boolean state.

import React, { useState } from 'react';

function Welcome() {
  const [isLoggedIn, setIsLoggedIn] = useState(false);
  return (
    <div>
      {isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign in.</h1>}
    </div>
  );
}

What will be rendered when this component first appears?

React
import React, { useState } from 'react';

function Welcome() {
  const [isLoggedIn, setIsLoggedIn] = useState(false);
  return (
    <div>
      {isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign in.</h1>}
    </div>
  );
}
A<div><h1>Please sign in.</h1></div>
B<div><h1>Welcome back!</h1></div>
C<div></div>
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint

Look at the initial value of isLoggedIn in the useState hook.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses conditional rendering with && operator?

In React, you want to render a <p>Hello User</p> only if isUser is true. Which code snippet correctly does this?

React
function Greeting({ isUser }) {
  return (
    <div>
      {/* Your code here */}
    </div>
  );
}
A{isUser || <p>Hello User</p>}
B{isUser && <p>Hello User</p>}
C{if (isUser) <p>Hello User</p>}
D{isUser ? <p>Hello User</p> : null}
Attempts:
2 left
💡 Hint

Remember that JSX expressions must be valid JavaScript expressions.

❓ state_output
advanced
2:00remaining
What is the rendered output after clicking the button twice?

Given this React component, what will be the displayed message after clicking the button two times?

import React, { useState } from 'react';

function ToggleMessage() {
  const [show, setShow] = useState(true);
  return (
    
{show &&

Visible

}
); }
React
import React, { useState } from 'react';

function ToggleMessage() {
  const [show, setShow] = useState(true);
  return (
    <div>
      {show && <p>Visible</p>}
      <button onClick={() => setShow(!show)}>Toggle</button>
    </div>
  );
}
A<div><button>Toggle</button></div>
B<div><p>Visible</p><p>Visible</p><button>Toggle</button></div>
C<div><p>Toggle</p><button>Visible</button></div>
D<div><p>Visible</p><button>Toggle</button></div>
Attempts:
2 left
💡 Hint

Each click toggles the show state between true and false.

🔧 Debug
advanced
2:00remaining
Why does this conditional rendering cause an error?

Examine this React component:

function Example({ show }) {
  return (
    <div>
      {show && if (true) <p>Hello</p>}
    </div>
  );
}

What error will this code cause?

React
function Example({ show }) {
  return (
    <div>
      {show && if (true) <p>Hello</p>}
    </div>
  );
}
ASyntaxError: Unexpected token 'if'
BTypeError: Cannot read property 'p' of undefined
CNo error, renders <p>Hello</p> when show is true
DReferenceError: show is not defined
Attempts:
2 left
💡 Hint

JSX expressions must be valid JavaScript expressions, not statements.

🧠 Conceptual
expert
2:00remaining
Which option best describes React's conditional rendering behavior?

In React, when you use conditional rendering with expressions like {condition && <Component />}, what happens if condition is false?

AReact renders the component but hides it with CSS
BReact throws an error because false is not a valid React element
CReact renders nothing for that expression, outputting false which is ignored in rendering
DReact renders a placeholder element instead of the component
Attempts:
2 left
💡 Hint

Think about how React treats boolean values in JSX.

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

  1. Step 1: Understand the purpose of conditional rendering

    Conditional rendering controls what parts of the UI appear based on conditions like state or props.
  2. Step 2: Differentiate from other React features

    Changing styles or fetching data are different tasks; conditional rendering specifically controls visibility.
  3. Final Answer:

    Show or hide parts of the UI based on data or state -> Option D
  4. 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

  1. Step 1: Recall JSX expression syntax

    JSX uses curly braces {} to embed JavaScript expressions, including ternary operators.
  2. Step 2: Identify correct ternary operator usage

    The ternary operator syntax is condition ? exprIfTrue : exprIfFalse, which matches {condition ? <ComponentA /> : <ComponentB />}.
  3. Final Answer:

    {condition ? <ComponentA /> : <ComponentB />} -> Option B
  4. 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?
function Greeting({ isLoggedIn }) {
  return (
    <div>
      {isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign up.</h1>}
    </div>
  );
}
medium
A.

Welcome back!

B. Syntax error
C.

Please sign up.

D. Nothing will render

Solution

  1. Step 1: Check the value of isLoggedIn

    The value is false, so the condition in the ternary operator is false.
  2. Step 2: Evaluate the ternary operator

    Since condition is false, the expression after the colon is rendered: <h1>Please sign up.</h1>.
  3. Final Answer:

    <h1>Please sign up.</h1> -> Option C
  4. Quick Check:

    isLoggedIn false = show sign up message [OK]
Hint: False condition shows expression after colon in ternary [OK]
Common Mistakes:
  • Assuming true branch renders when condition is false
  • Thinking nothing renders on false
  • Confusing syntax with runtime error
4. Identify the error in this React component that tries to conditionally render a message:
function Message({ show }) {
  return (
    <div>
      {show && <p>Hello World!</p> else <p>No message</p>}
    </div>
  );
}
medium
A. Using else inside JSX expression is invalid
B. Missing parentheses around JSX elements
C. Incorrect use of logical AND operator
D. Component must return a string, not JSX

Solution

  1. Step 1: Review JSX expression syntax

    JSX expressions inside curly braces must be valid JavaScript expressions; 'else' cannot be used inside them.
  2. Step 2: Identify invalid use of else

    The code uses 'else' after a logical AND expression, which is not valid JavaScript syntax.
  3. Final Answer:

    Using else inside JSX expression is invalid -> Option A
  4. Quick Check:

    JSX expressions cannot contain else keyword [OK]
Hint: No else keyword inside JSX expressions; use ternary instead [OK]
Common Mistakes:
  • Trying to use else with logical AND
  • Forgetting ternary operator syntax
  • Assuming JSX can contain statements like else
5. You want to render a list of notifications only if the list is not empty. Which React pattern correctly handles this conditional rendering?
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>
  );
}
hard
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

  1. 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.
  2. 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.
  3. Final Answer:

    Use ternary operator checking notifications.length > 0 -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard