Challenge - 5 Problems
useCallback Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate1:30remaining
Why use useCallback in React Native?
You have a button component that receives a function prop. Without useCallback, what problem might occur when the parent re-renders?
Attempts:
2 left
💡 Hint
Think about how JavaScript compares functions passed as props.
✗ Incorrect
Without useCallback, a new function is created on every render, causing child components to think props changed and re-render.
❓ lifecycle
intermediate1:30remaining
useCallback dependencies effect
What happens if you omit dependencies in useCallback like this: useCallback(() => doSomething(), [])?
Attempts:
2 left
💡 Hint
Consider what an empty dependency array means for memoization.
✗ Incorrect
An empty dependency array means the function is created once and never updated, so if doSomething changes, the callback still uses the old version.
📝 Syntax
advanced2:00remaining
Identify the correct useCallback syntax
Which option shows the correct syntax for useCallback that depends on a variable count?
React Native
const memoizedCallback = useCallback(() => {
console.log(count);
}, [count]);Attempts:
2 left
💡 Hint
Remember useCallback takes a function and an array of dependencies.
✗ Incorrect
Option C correctly passes a function and an array with count as dependency. Option C passes count directly, not in an array. Option C omits dependencies. Option C calls console.log immediately.
🔧 Debug
advanced2:00remaining
Why does this useCallback cause stale state?
Given this code snippet, why does the alert always show 0?
const [count, setCount] = useState(0);
const showAlert = useCallback(() => {
alert(count);
}, []);
Attempts:
2 left
💡 Hint
Think about what an empty dependency array means for captured variables.
✗ Incorrect
The callback is created once with count = 0 and never updates, so it always alerts 0 even if count changes later.
🧠 Conceptual
expert2:30remaining
When is useCallback NOT helpful?
In which scenario does using useCallback NOT improve performance in React Native?
Attempts:
2 left
💡 Hint
Consider when memoizing a function actually prevents re-renders.
✗ Incorrect
If the child component does not memoize props, passing a memoized function does not prevent re-renders, so useCallback adds unnecessary complexity.