Conditional rendering lets React components show different UI parts depending on conditions like user input or data state. This makes apps interactive and dynamic.
isLoggedIn is false?function Greeting({ isLoggedIn }) { return ( <div> {isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign up.</h1>} </div> ); }
The component uses a conditional (ternary) operator. When isLoggedIn is false, it renders <h1>Please sign up.</h1>.
import React, { useState } from 'react'; function ToggleMessage() { const [show, setShow] = useState(false); return ( <div> <button onClick={() => setShow(!show)}>Toggle</button> {show && <p>Hello!</p>} </div> ); }
The show state starts as false. Clicking toggles it: false → true → false. After two clicks, show is false, so <p>Hello!</p> is not rendered.
<p>Loading...</p> only when isLoading is true?Option D uses the logical AND operator to conditionally render the paragraph when isLoading is true. Options A, C, and D have syntax errors or invalid JSX.
function Example({ show }) { if (show) { return <p>Visible</p>; } else { return <p>Hidden</p>; } }
The component uses if-else blocks with JSX inside but does not return anything. React components must return JSX or null. Without a return, it returns undefined, causing an error.