Challenge - 5 Problems
React State Mastery
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 that uses state. What will be displayed when the button is clicked once?
React
import React, { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); return ( <> <p>Count: {count}</p> <button onClick={() => setCount(count + 1)}>Increase</button> </> ); } export default Counter;
Attempts:
2 left
💡 Hint
Think about what happens when the button's onClick updates the state from 0 to 1.
✗ Incorrect
The initial state count is 0. When the button is clicked, setCount(count + 1) updates it to 1. The component re-renders showing Count: 1.
❓ state_output
intermediate2:00remaining
What is the value of count after two button clicks?
Given this React component, what will be the value of
count after clicking the button twice quickly?React
import React, { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); const handleClick = () => { setCount(count + 1); setCount(count + 1); }; return ( <> <p>Count: {count}</p> <button onClick={handleClick}>Increase Twice</button> </> ); } export default Counter;
Attempts:
2 left
💡 Hint
Remember that state updates may be batched and the count variable does not update immediately inside the function.
✗ Incorrect
Both setCount(count + 1) calls use the same count value (0). So the state updates to 1, not 2.
📝 Syntax
advanced2:00remaining
Which option correctly initializes state with an object in React?
You want to store a user's name and age in state as an object. Which code correctly initializes this state?
React
import React, { useState } from 'react'; function UserInfo() { // Initialize state here return null; } export default UserInfo;
Attempts:
2 left
💡 Hint
State should be initialized with a single value, often an object or primitive, not multiple separate variables.
✗ Incorrect
Option A correctly uses an object literal to initialize state. Options A and C use wrong types, and D is invalid syntax.
🔧 Debug
advanced2:00remaining
Why does this React component not update the displayed count?
Look at this React component. Why does clicking the button not change the displayed count?
React
import React, { useState } from 'react'; function Counter() { let count = 0; const handleClick = () => { count = count + 1; }; return ( <> <p>Count: {count}</p> <button onClick={handleClick}>Increase</button> </> ); } export default Counter;
Attempts:
2 left
💡 Hint
React only re-renders when state or props change, not when local variables change.
✗ Incorrect
Local variables like count do not cause React to re-render. State must be used to trigger updates.
🧠 Conceptual
expert2:00remaining
What is the main purpose of state in React components?
Choose the best description of what state is used for in React components.
Attempts:
2 left
💡 Hint
Think about what changes in a component cause it to update its display.
✗ Incorrect
State holds data that can change and causes the component to update its output. It is local to the component and not global.