0
0
Reactframework~20 mins

If conditions in JSX in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSX Condition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this React component render?
Consider this React component using an if condition inside JSX. What will it display when isLoggedIn is true?
React
function Greeting({ isLoggedIn }) {
  return (
    <div>
      {isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign up.</h1>}
    </div>
  );
}
A<div></div>
B<div><h1>Please sign up.</h1></div>
C<div><h1>Welcome back!</h1></div>
DSyntaxError: Unexpected token '?'
Attempts:
2 left
💡 Hint
Look at the ternary operator inside the JSX and what happens when the condition is true.
📝 Syntax
intermediate
2:00remaining
Which option correctly uses an if statement inside JSX?
You want to conditionally render a message only if showMessage is true. Which code snippet is valid React JSX?
Areturn (<div>{showMessage && <p>Hello!</p>}</div>);
Breturn (<div>{showMessage ? <p>Hello!</p> : null}</div>);
Creturn (<div>{showMessage ? <p>Hello!</p> : }</div>);
Dreturn (<div>{if(showMessage) { <p>Hello!</p> }}</div>);
Attempts:
2 left
💡 Hint
Remember that JSX expressions cannot contain statements like if directly.
🔧 Debug
advanced
2:00remaining
Why does this React component cause an error?
Look at this component code. Why does it cause a syntax error?
React
function Status({ isOnline }) {
  return (
    <div>
      {if (isOnline) {
        <span>Online</span>
      } else {
        <span>Offline</span>
      }}
    </div>
  );
}
AThe <span> tags are not closed properly.
BJSX does not allow if statements inside curly braces; only expressions are allowed.
CThe component is missing a return statement.
DThe isOnline prop is not defined.
Attempts:
2 left
💡 Hint
Think about what JSX allows inside curly braces.
state_output
advanced
2:00remaining
What is the output after clicking the button twice?
This React component toggles a message when the button is clicked. What will it display after two clicks?
React
import React, { useState } from 'react';

function ToggleMessage() {
  const [show, setShow] = useState(false);
  return (
    <div>
      <button onClick={() => setShow(!show)}>Toggle</button>
      {show && <p>The message is visible</p>}
    </div>
  );
}
A<div><button>Toggle</button><p></p></div>
B<div><button>Toggle</button><p>The message is visible</p><p>The message is visible</p></div>
C<div><button>Toggle</button><p>The message is visible</p></div>
D<div><button>Toggle</button></div>
Attempts:
2 left
💡 Hint
Each click toggles the boolean state. Think about the initial state and toggling twice.
🧠 Conceptual
expert
3:00remaining
Which approach correctly handles multiple if conditions in JSX?
You want to render different greetings based on the time of day using JSX. Which option correctly implements multiple conditions?
Areturn (<div>{hour < 12 ? <p>Good morning</p> : hour < 18 ? <p>Good afternoon</p> : <p>Good evening</p>}</div>);
Breturn (<div>{hour < 12 && <p>Good morning</p>} {hour >= 12 && hour < 18 && <p>Good afternoon</p>} {hour >= 18 && <p>Good evening</p>}</div>);
Creturn (<div>{if (hour < 12) {<p>Good morning</p>} else if (hour < 18) {<p>Good afternoon</p>} else {<p>Good evening</p>}}</div>);
Dreturn (<div>{hour < 12 ? <p>Good morning</p> : if (hour < 18) {<p>Good afternoon</p>} else {<p>Good evening</p>}}</div>);
Attempts:
2 left
💡 Hint
JSX allows nested ternary operators but not if statements inside curly braces.