Bird
Raised Fist0
React Nativemobile~20 mins

useCallback optimization in React Native - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is the main purpose of useCallback in React Native?
easy
A. To create new state variables
B. To prevent functions from being recreated on every render
C. To style components dynamically
D. To fetch data from an API

Solution

  1. Step 1: Understand function recreation in React

    Functions inside components are recreated on every render by default, which can cause unnecessary work.
  2. Step 2: Role of useCallback

    useCallback memoizes the function, so it keeps the same reference unless dependencies change.
  3. Final Answer:

    To prevent functions from being recreated on every render -> Option B
  4. Quick Check:

    useCallback = prevent function recreation [OK]
Hint: useCallback keeps functions stable between renders [OK]
Common Mistakes:
  • Thinking useCallback creates state
  • Confusing useCallback with styling
  • Assuming useCallback fetches data
2. Which of the following is the correct syntax to use useCallback?
easy
A. const memoizedFn = useCallback(doSomething, dependency);
B. const memoizedFn = useCallback(() => { doSomething(); });
C. const memoizedFn = useCallback(() => { doSomething(); }, [dependency]);
D. const memoizedFn = useCallback(doSomething, [dependency]);

Solution

  1. Step 1: Check useCallback parameters

    useCallback takes a function and an array of dependencies as parameters.
  2. Step 2: Validate correct syntax

    const memoizedFn = useCallback(() => { doSomething(); }, [dependency]); correctly passes an arrow function and an array of dependencies.
  3. Final Answer:

    const memoizedFn = useCallback(() => { doSomething(); }, [dependency]); -> Option C
  4. Quick Check:

    useCallback syntax = function + [dependencies] [OK]
Hint: useCallback needs a function and dependency array [OK]
Common Mistakes:
  • Omitting dependency array
  • Passing dependencies without array brackets
  • Passing function reference without wrapping in arrow function
3. What will be the output of the following code snippet?
const [count, setCount] = useState(0);
const increment = useCallback(() => setCount(count + 1), []);

increment();
increment();
console.log(count);
medium
A. 2
B. Error
C. 1
D. 0

Solution

  1. Step 1: Analyze useCallback dependencies

    The callback uses count but has an empty dependency array, so it captures the initial value 0.
  2. Step 2: Effect of calling increment()

    Calling increment() calls setCount(0 + 1) twice, but React batches updates and count remains 0 in this synchronous context.
  3. Final Answer:

    0 -> Option D
  4. Quick Check:

    Empty dependencies cause stale count = 0 [OK]
Hint: Empty deps cause stale closure values [OK]
Common Mistakes:
  • Assuming count updates immediately
  • Ignoring stale closure from empty deps
  • Expecting increment to add twice
4. Identify the issue in this code and how to fix it:
const [value, setValue] = useState(0);
const updateValue = useCallback(() => {
  setValue(value + 1);
}, []);
medium
A. Missing dependency 'value' in useCallback array; add [value]
B. setValue should be outside useCallback
C. useCallback should not be used here
D. No issue; code is correct

Solution

  1. Step 1: Check dependencies in useCallback

    The callback uses value but the dependency array is empty, causing stale closure.
  2. Step 2: Fix by adding dependency

    Add value to the dependency array to update the callback when value changes.
  3. Final Answer:

    Missing dependency 'value' in useCallback array; add [value] -> Option A
  4. Quick Check:

    Dependencies must include all used variables [OK]
Hint: Always include variables used inside useCallback deps [OK]
Common Mistakes:
  • Leaving dependency array empty
  • Removing useCallback instead of fixing deps
  • Misplacing setValue call
5. You have a parent component passing a callback to a child. The callback depends on multiple state variables. How should you optimize it with useCallback to avoid unnecessary re-renders of the child?
hard
A. Wrap the callback with useCallback including all dependent state variables in the dependency array
B. Wrap the callback with useCallback but leave dependency array empty
C. Do not use useCallback; define the callback inline in the child
D. Use useMemo instead of useCallback for the callback

Solution

  1. Step 1: Understand callback dependencies

    The callback depends on multiple state variables, so it must update when any of them change.
  2. Step 2: Use useCallback with all dependencies

    Include all dependent state variables in the dependency array to keep the callback updated and stable.
  3. Step 3: Avoid empty dependency array

    Leaving it empty causes stale values and bugs; defining inline causes new function each render.
  4. Final Answer:

    Wrap the callback with useCallback including all dependent state variables in the dependency array -> Option A
  5. Quick Check:

    All dependencies in useCallback deps = stable, updated callback [OK]
Hint: Include all used states in useCallback deps to optimize [OK]
Common Mistakes:
  • Leaving deps empty causing stale closures
  • Defining callback inline causing re-renders
  • Confusing useMemo with useCallback
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