0
0
React Nativemobile~20 mins

useCallback optimization in React Native - 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!
ui_behavior
intermediate
1: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?
AThe button will never re-render even if props change.
BThe button re-renders unnecessarily because the function prop changes reference each time.
CThe app crashes due to infinite loops.
DThe function prop becomes undefined.
Attempts:
2 left
💡 Hint
Think about how JavaScript compares functions passed as props.
lifecycle
intermediate
1:30remaining
useCallback dependencies effect
What happens if you omit dependencies in useCallback like this: useCallback(() => doSomething(), [])?
AThe callback never updates even if doSomething changes, possibly causing stale closures.
BThe callback updates every render automatically.
CThe callback runs immediately on render.
DIt causes a syntax error.
Attempts:
2 left
💡 Hint
Consider what an empty dependency array means for memoization.
📝 Syntax
advanced
2: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]);
Aconst memoizedCallback = useCallback(() => console.log(count));
Bconst memoizedCallback = useCallback(() => { console.log(count) }, count);
Cconst memoizedCallback = useCallback(() => console.log(count), [count]);
Dconst memoizedCallback = useCallback(console.log(count), [count]);
Attempts:
2 left
💡 Hint
Remember useCallback takes a function and an array of dependencies.
🔧 Debug
advanced
2: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); }, []);
ABecause useCallback has an empty dependency array, it captures count as 0 and never updates.
BBecause alert is asynchronous and shows old value.
CBecause setCount was never called.
DBecause useState is not initialized properly.
Attempts:
2 left
💡 Hint
Think about what an empty dependency array means for captured variables.
🧠 Conceptual
expert
2:30remaining
When is useCallback NOT helpful?
In which scenario does using useCallback NOT improve performance in React Native?
AWhen the function is expensive to create.
BWhen the function depends on many changing dependencies.
CWhen the function is used inside useEffect.
DWhen the function is passed to a child that does not use React.memo or similar memoization.
Attempts:
2 left
💡 Hint
Consider when memoizing a function actually prevents re-renders.