Challenge - 5 Problems
Reusable UI Components 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 reusable button component?
Consider this React component that accepts a
label and onClick function as props. What will be displayed and how will it behave when clicked?React
import React from 'react'; function Button({ label, onClick }) { return <button onClick={onClick}>{label}</button>; } export default function App() { const handleClick = () => alert('Clicked!'); return <Button label="Press me" onClick={handleClick} />; }
Attempts:
2 left
💡 Hint
Look at the props passed to the Button component and what the onClick function does.
✗ Incorrect
The Button component renders a button with the given label and calls the onClick function when clicked. Here, label is 'Press me' and onClick shows an alert 'Clicked!'.
❓ state_output
intermediate2:00remaining
What is the displayed count after clicking the button twice?
This reusable counter component uses React state. What number will be shown after clicking the button two times?
React
import React, { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); return ( <> <p>Count: {count}</p> <button onClick={() => setCount(count + 1)}>Increment</button> </> ); } export default Counter;
Attempts:
2 left
💡 Hint
Each click increases count by 1 starting from 0.
✗ Incorrect
The count starts at 0. Each click adds 1. After two clicks, count is 2.
📝 Syntax
advanced2:30remaining
Which option correctly defines a reusable input component with controlled value?
Choose the code that correctly implements a controlled input component in React that updates its value on user typing.
Attempts:
2 left
💡 Hint
Controlled inputs use the value prop and update via onChange with event target value.
✗ Incorrect
Option A correctly passes value and calls onChange with the new input value. Option A uses defaultValue which is uncontrolled. Option A passes onChange directly without extracting value. Option A lacks value prop making it uncontrolled.
🔧 Debug
advanced2:30remaining
Why does this reusable list component not update when items change?
This component receives an array of items as props and renders them. However, when the items array changes, the list does not update. What is the likely cause?
React
function ItemList({ items }) { const [list, setList] = React.useState(items); return <ul>{list.map(item => <li key={item.id}>{item.name}</li>)}</ul>; }
Attempts:
2 left
💡 Hint
State initialized from props does not update automatically when props change.
✗ Incorrect
The component sets state from props once on mount. Later changes to props do not update state, so the list stays the same.
🧠 Conceptual
expert1:30remaining
Which statement best describes the benefit of reusable UI components?
Why do developers create reusable UI components in frameworks like React?
Attempts:
2 left
💡 Hint
Think about code reuse and maintenance benefits.
✗ Incorrect
Reusable components let developers build UI pieces once and reuse them, making apps consistent and easier to maintain.