Challenge - 5 Problems
Props Destructuring Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this React component?
Consider this React component using props destructuring. What will it render?
React
function Greeting({ name, age }) { return <p>Hello, {name}! You are {age} years old.</p>; } // Usage: <Greeting name="Alice" age={30} />
Attempts:
2 left
💡 Hint
Look at how props are unpacked directly in the function parameters.
✗ Incorrect
The component uses destructuring to get 'name' and 'age' from props. So it replaces {name} with 'Alice' and {age} with 30.
📝 Syntax
intermediate2:00remaining
Which option correctly destructures props in a functional component?
You want to extract 'title' and 'content' from props in a React functional component. Which code is correct?
Attempts:
2 left
💡 Hint
Destructuring can happen directly in the function parameters.
✗ Incorrect
Option B shows destructuring directly in the parameter list, which is a common and concise pattern.
🔧 Debug
advanced2:00remaining
Why does this component throw an error?
This React component tries to destructure props but causes an error. Why?
React
function UserProfile({ user }) { const { name, age } = user; return <p>{name} is {age} years old.</p>; } // Usage: <UserProfile />
Attempts:
2 left
💡 Hint
Think about what happens if 'user' prop is missing.
✗ Incorrect
If 'user' prop is missing, it is undefined. Trying to destructure properties from undefined causes an error.
❓ state_output
advanced2:00remaining
What is the output after clicking the button?
This React component uses props destructuring and state. What will it display after clicking the button once?
React
import { useState } from 'react'; function Counter({ initial }) { const [count, setCount] = useState(initial); return ( <> <p>Count: {count}</p> <button onClick={() => setCount(count + 1)}>Increment</button> </> ); } // Usage: <Counter initial={5} />
Attempts:
2 left
💡 Hint
The initial state is set from the 'initial' prop, then incremented by 1 on click.
✗ Incorrect
The state starts at 5 from the prop. Clicking the button adds 1, so count becomes 6.
🧠 Conceptual
expert2:00remaining
Why prefer props destructuring in React components?
Which is the best reason to use props destructuring in React functional components?
Attempts:
2 left
💡 Hint
Think about how destructuring affects code readability.
✗ Incorrect
Destructuring props helps write cleaner code by extracting needed values directly, avoiding repeated 'props.' prefixes.