0
0
Reactframework~20 mins

Callback functions for state updates in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Callback State Master
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 that updates state using a callback function:

import React, { useState } from 'react';

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

  function increment() {
    setCount(prevCount => prevCount + 1);
  }

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
    </div>
  );
}

If the user clicks the button twice quickly, what will be the displayed count?

React
import React, { useState } from 'react';

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

  function increment() {
    setCount(prevCount => prevCount + 1);
  }

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
    </div>
  );
}
ACount: NaN
BCount: 1
CCount: 0
DCount: 2
Attempts:
2 left
💡 Hint

Think about how React batches state updates and how the callback function uses the previous state.

📝 Syntax
intermediate
1:30remaining
Which option correctly uses a callback to update state based on previous value?

Choose the correct way to update a React state variable score by adding 5 using a callback function.

AsetScore(prevScore => prevScore + 5);
BsetScore(() => score + 5);
CsetScore(score + 5);
DsetScore(prevScore + 5);
Attempts:
2 left
💡 Hint

Remember the callback receives the previous state as an argument.

🔧 Debug
advanced
2:30remaining
Why does this state update not increment as expected?

Look at this React component:

function Clicker() {
  const [clicks, setClicks] = React.useState(0);

  function handleClick() {
    setClicks(clicks + 1);
    setClicks(clicks + 1);
  }

  return (
    <button onClick={handleClick}>Clicks: {clicks}</button>
  );
}

After clicking the button once, what is the value of clicks and why?

React
function Clicker() {
  const [clicks, setClicks] = React.useState(0);

  function handleClick() {
    setClicks(clicks + 1);
    setClicks(clicks + 1);
  }

  return (
    <button onClick={handleClick}>Clicks: {clicks}</button>
  );
}
AClicks: 2 because setClicks is called twice
BClicks: 1 because both setClicks use the same stale clicks value
CClicks: 0 because state does not update on click
DClicks: NaN due to invalid state update
Attempts:
2 left
💡 Hint

Consider how React batches state updates and the value of clicks inside the function.

component_behavior
advanced
2:00remaining
What will be the final count after these updates?

Given this React component:

function MultiIncrement() {
  const [count, setCount] = React.useState(0);

  function incrementThrice() {
    setCount(c => c + 1);
    setCount(c => c + 1);
    setCount(c => c + 1);
  }

  return (
    <button onClick={incrementThrice}>Count: {count}</button>
  );
}

What will the count show after clicking the button once?

React
function MultiIncrement() {
  const [count, setCount] = React.useState(0);

  function incrementThrice() {
    setCount(c => c + 1);
    setCount(c => c + 1);
    setCount(c => c + 1);
  }

  return (
    <button onClick={incrementThrice}>Count: {count}</button>
  );
}
ACount: 3
BCount: NaN
CCount: 0
DCount: 1
Attempts:
2 left
💡 Hint

Each callback receives the updated state from the previous callback.

🧠 Conceptual
expert
1:30remaining
Why prefer callback functions for state updates in React?

Which reason best explains why React recommends using callback functions with setState when new state depends on previous state?

ACallback functions automatically debounce rapid state changes
BCallback functions prevent unnecessary re-renders by skipping updates
CCallback functions ensure updates use the latest state value despite batching
DCallback functions allow direct mutation of state for performance
Attempts:
2 left
💡 Hint

Think about how React batches multiple state updates asynchronously.