Bird
Raised Fist0
React Nativemobile~5 mins

useCallback optimization in React Native - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the purpose of useCallback in React Native?

useCallback helps remember a function so it doesn’t get recreated on every render. This saves work and can make your app faster.

Click to reveal answer
beginner
When should you use useCallback?

Use it when you pass functions to child components that depend on props or state, to avoid unnecessary re-renders.

Click to reveal answer
intermediate
What happens if you forget to add dependencies in useCallback?

The function may use old values and cause bugs because it won’t update when those values change.

Click to reveal answer
intermediate
How does useCallback help with React Native's FlatList performance?

By memoizing item render functions, useCallback prevents unnecessary re-renders of list items, making scrolling smoother.

Click to reveal answer
advanced
Can useCallback always improve performance?

No. If the function is simple or not passed to children, useCallback might add extra work and slow things down.

Click to reveal answer
What does useCallback return?
AA state variable
BA new component
CA memoized version of the callback function
DA side effect
Which of these is a correct use of useCallback?
Aconst memoFn = useCallback(() => doSomething(), [dependency]);
Bconst memoFn = useCallback(doSomething());
Cconst memoFn = useCallback(doSomething, []);
Dconst memoFn = useCallback(() => doSomething());
What is a common mistake when using useCallback?
ANot including dependencies in the array
BUsing it for state variables
CCalling it inside loops
DReturning JSX from it
How does useCallback help child components?
AMakes them load faster
BPrevents them from re-rendering if props don’t change
CChanges their state automatically
DRemoves them from the UI
When might useCallback hurt performance?
AWhen used with state setters
BWhen used with many dependencies
CWhen used inside <code>useEffect</code>
DWhen used on simple functions that don’t change
Explain how useCallback helps optimize React Native apps.
Think about how functions passed to children can cause extra work.
You got /4 concepts.
    Describe common mistakes when using useCallback and their effects.
    What happens if the function uses old values?
    You got /4 concepts.

      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