Challenge - 5 Problems
useState Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output after clicking the button twice?
Consider this React component using the
useState hook. What will be the displayed count after clicking the button two times?React
import React, { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); function handleClick() { setCount(count + 1); } return ( <div> <p>Count: {count}</p> <button onClick={handleClick}>Increment</button> </div> ); } export default Counter;
Attempts:
2 left
💡 Hint
Remember that each click increases the count by 1 starting from 0.
✗ Incorrect
The initial count is 0. Each click calls setCount(count + 1), increasing count by 1. After two clicks, count becomes 2.
❓ state_output
intermediate2:00remaining
What is the value of count after this sequence?
Given this React component, what is the value of
count after the button is clicked once?React
import React, { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); function handleClick() { setCount(prevCount => prevCount + 1); } return ( <div> <p>Count: {count}</p> <button onClick={handleClick}>Increment</button> </div> ); } export default Counter;
Attempts:
2 left
💡 Hint
The updater function uses the previous count value.
✗ Incorrect
The updater function increments the previous count by 1. Starting from 0, after one click, count becomes 1.
📝 Syntax
advanced2:00remaining
Which option correctly initializes state with useState?
Which of these options correctly initializes a state variable
name with the value 'Alice' using the useState hook?Attempts:
2 left
💡 Hint
Remember that useState returns an array with two elements.
✗ Incorrect
Option B correctly uses array destructuring to get the state and setter from useState. Other options have syntax errors or misuse the hook.
🔧 Debug
advanced2:00remaining
Why does this component not update the count on button click?
This React component uses useState but the count does not increase when the button is clicked. What is the cause?
React
import React, { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); function handleClick() { count = count + 1; } return ( <div> <p>Count: {count}</p> <button onClick={handleClick}>Increment</button> </div> ); } export default Counter;
Attempts:
2 left
💡 Hint
State variables are read-only; you must use the setter function.
✗ Incorrect
State variables from useState cannot be changed directly. You must call setCount to update the state and trigger a re-render.
🧠 Conceptual
expert3:00remaining
What happens if you call setState multiple times synchronously?
In React, if you call
setCount(count + 1) three times in a row inside one function, what will be the final value of count assuming initial value 0?React
import React, { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); function handleClick() { setCount(count + 1); setCount(count + 1); setCount(count + 1); } return ( <div> <p>Count: {count}</p> <button onClick={handleClick}>Increment</button> </div> ); } export default Counter;
Attempts:
2 left
💡 Hint
State updates inside event handlers are batched and use the current state value at the time of the event.
✗ Incorrect
All three setCount calls use the same 'count' value (0) and set it to 1. To increment correctly multiple times, use the functional updater form.