0
0
Reactframework~20 mins

useCallback hook in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
useCallback Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this React component using useCallback?

Consider this React component that uses useCallback. What will be logged when the button is clicked twice?

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

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

  const increment = useCallback(() => {
    console.log('Count before increment:', count);
    setCount(count + 1);
  }, []);

  return (
    <button onClick={increment}>Increment</button>
  );
}

export default Counter;
ALogs 'Count before increment: 0' both times the button is clicked.
BLogs 'Count before increment: 0' first click, then 'Count before increment: 1' second click.
CLogs 'Count before increment: 1' both times the button is clicked.
DLogs 'Count before increment: 0' first click, then 'Count before increment: 2' second click.
Attempts:
2 left
💡 Hint

Remember that useCallback with an empty dependency array keeps the same function instance and closes over the initial count value.

state_output
intermediate
2:00remaining
What is the value of count after clicking the button twice?

Given this React component, what will be the value of count after clicking the button two times?

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

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

  const increment = useCallback(() => {
    setCount(prev => prev + 1);
  }, []);

  return (
    <button onClick={increment}>Increment</button>
  );
}

export default Counter;
Acount will be 1
Bcount will be 2
Ccount will be 0
Dcount will be undefined
Attempts:
2 left
💡 Hint

Look at how setCount uses a function to update state.

📝 Syntax
advanced
2:00remaining
Which option correctly uses useCallback to memoize a function depending on a prop?

Which code snippet correctly memoizes handleClick so it updates when propValue changes?

Aconst handleClick = useCallback(() => { console.log(propValue); }, []);
Bconst handleClick = useCallback(() => { console.log(propValue); }, [setPropValue]);
Cconst handleClick = useCallback(() => { console.log(propValue); });
Dconst handleClick = useCallback(() => { console.log(propValue); }, [propValue]);
Attempts:
2 left
💡 Hint

Think about which dependencies should be included to update the callback when props change.

🔧 Debug
advanced
2:00remaining
Why does this useCallback hook cause a stale closure bug?

Examine this code. Why does the handleClick function always log the initial value of count?

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

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

  const handleClick = useCallback(() => {
    console.log(count);
  }, []);

  return (
    <>
      <button onClick={() => setCount(count + 1)}>Increase</button>
      <button onClick={handleClick}>Log Count</button>
    </>
  );
}

export default Counter;
ABecause count is updated asynchronously, the console.log always shows the previous value.
BBecause setCount is not included in dependencies, the function does not update.
CBecause useCallback has an empty dependency array, it captures the initial count value and never updates.
DBecause handleClick is not called inside useEffect, it does not update.
Attempts:
2 left
💡 Hint

Think about how dependency arrays affect closures in hooks.

🧠 Conceptual
expert
2:00remaining
Which statement best describes the purpose of useCallback in React?

Choose the best explanation for why and when to use useCallback in React components.

A<p>useCallback memoizes a function so it keeps the same reference between renders unless dependencies change, helping to avoid unnecessary re-renders in child components.</p>
B<p>useCallback automatically batches multiple state updates into one render to improve performance.</p>
C<p>useCallback forces React to re-render a component whenever a function changes.</p>
D<p>useCallback replaces useEffect to run side effects only when dependencies change.</p>
Attempts:
2 left
💡 Hint

Think about how React compares props and why function identity matters.