0
0
Reactframework~20 mins

useState hook introduction in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
useState Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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;
ACount: 3
BCount: 1
CCount: 2
DCount: 0
Attempts:
2 left
💡 Hint
Remember that each click increases the count by 1 starting from 0.
state_output
intermediate
2: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;
ACount: 1
BCount: 0
CCount: NaN
DCount: undefined
Attempts:
2 left
💡 Hint
The updater function uses the previous count value.
📝 Syntax
advanced
2: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?
Aconst name = useState('Alice');
Bconst [name, setName] = useState('Alice');
Cconst [name, setName] = useState = 'Alice';
Dconst [name, setName] = useState('Alice')()
Attempts:
2 left
💡 Hint
Remember that useState returns an array with two elements.
🔧 Debug
advanced
2: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;
AuseState is not imported correctly.
BThe button's onClick handler is missing parentheses.
CThe initial state value must be a function.
DDirectly modifying 'count' does not update state; setCount must be used.
Attempts:
2 left
💡 Hint
State variables are read-only; you must use the setter function.
🧠 Conceptual
expert
3: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;
ACount will be 1 because state updates are batched and use the same initial count value.
BCount will be 3 because each setCount adds 1.
CCount will be 0 because setCount calls cancel each other.
DCount will be NaN due to multiple updates.
Attempts:
2 left
💡 Hint
State updates inside event handlers are batched and use the current state value at the time of the event.