Bird
Raised Fist0
Reactframework~20 mins

useState hook introduction in React - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the useState hook do in a React component?
easy
A. It fetches data from an external API automatically.
B. It allows the component to remember and update data over time.
C. It styles the component with CSS.
D. It handles routing between pages.

Solution

  1. Step 1: Understand the purpose of useState

    The useState hook is designed to let a React component keep track of data that can change.
  2. Step 2: Identify what useState does

    It returns the current value and a function to update that value, enabling the component to remember and update data.
  3. Final Answer:

    It allows the component to remember and update data over time. -> Option B
  4. Quick Check:

    useState manages component data = A [OK]
Hint: useState is for storing and changing data inside components [OK]
Common Mistakes:
  • Thinking useState fetches data from APIs
  • Confusing useState with styling or routing
  • Believing useState automatically updates the UI without calling the setter
2. Which of the following is the correct syntax to declare a state variable count with initial value 0 using useState?
easy
A. const [count, setCount] = useState(0);
B. const count = useState(0);
C. const count, setCount = useState(0);
D. const [count, setCount] = useState;

Solution

  1. Step 1: Recall useState syntax

    The useState hook returns an array with two elements: the current state and a function to update it, so destructuring with square brackets is needed.
  2. Step 2: Check each option

    const [count, setCount] = useState(0); correctly uses array destructuring and calls useState with initial value 0. Others either miss destructuring or call useState incorrectly.
  3. Final Answer:

    const [count, setCount] = useState(0); -> Option A
  4. Quick Check:

    useState returns [value, setter] = C [OK]
Hint: useState returns [value, setter] array, use destructuring [OK]
Common Mistakes:
  • Not using array destructuring
  • Forgetting to call useState as a function
  • Assigning useState to multiple variables without brackets
3. What will be displayed on the screen after clicking the button twice in this component?
import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  return (
    

Count: {count}

setCount(count + 1)}>Increment
); }
medium
A. Count: 0
B. Count: 1
C. Count: undefined
D. Count: 2

Solution

  1. Step 1: Understand initial state and button action

    The initial count is 0. Each button click calls setCount(count + 1), increasing count by 1.
  2. Step 2: Calculate count after two clicks

    After first click, count becomes 1. After second click, count becomes 2.
  3. Final Answer:

    Count: 2 -> Option D
  4. Quick Check:

    Two increments from 0 = 2 [OK]
Hint: Each click adds 1 to count starting from 0 [OK]
Common Mistakes:
  • Thinking count resets on each click
  • Confusing initial value with updated value
  • Ignoring that setCount updates state asynchronously
4. Identify the error in this React component using useState:
import React, { useState } from 'react';

function Example() {
  const [value, setValue] = useState;
  return (
    

{value}

setValue('Hello')}>Set
); }
medium
A. State variable value is not declared.
B. The setter function setValue is missing.
C. useState is not called as a function.
D. The button's onClick handler is invalid.

Solution

  1. Step 1: Check useState usage

    The code assigns useState without parentheses, so it is not called as a function, which is required.
  2. Step 2: Understand consequences

    Without calling useState(), value and setValue are undefined, causing errors.
  3. Final Answer:

    useState is not called as a function. -> Option C
  4. Quick Check:

    useState must be called with () = B [OK]
Hint: Always call useState with parentheses: useState() [OK]
Common Mistakes:
  • Forgetting parentheses after useState
  • Assuming useState is a variable, not a function
  • Misreading error as button or variable issue
5. You want to create a toggle button that switches text between "ON" and "OFF" using useState. Which code correctly implements this behavior?
hard
A. const [isOn, setIsOn] = useState(false); setIsOn(!isOn)}>{isOn ? 'ON' : 'OFF'}
B. const [isOn, setIsOn] = useState('OFF'); setIsOn('ON')}>{isOn}
C. const [isOn, setIsOn] = useState(true); setIsOn(true)}>{isOn ? 'ON' : 'OFF'}
D. const isOn = false; isOn = !isOn}>{isOn ? 'ON' : 'OFF'}

Solution

  1. Step 1: Understand toggle logic with useState

    To toggle, state must be boolean and updated to its opposite on click.
  2. Step 2: Evaluate each option

    const [isOn, setIsOn] = useState(false); setIsOn(!isOn)}>{isOn ? 'ON' : 'OFF'} initializes state as false and toggles it with !isOn, displaying 'ON' or 'OFF' accordingly. Others either don't toggle or misuse state.
  3. Final Answer:

    const [isOn, setIsOn] = useState(false); setIsOn(!isOn)}>{isOn ? 'ON' : 'OFF'} -> Option A
  4. Quick Check:

    Toggle state by flipping boolean = A [OK]
Hint: Toggle boolean state by setting it to !currentValue [OK]
Common Mistakes:
  • Using string state without toggling
  • Setting state to same value repeatedly
  • Trying to mutate state variable directly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard