0
0
Reactframework~20 mins

Conditional component rendering in React - Practice Problems & Coding Challenges

Choose your learning style9 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.