useCallback helps your app remember a function so it doesn't get recreated every time the screen updates. This makes your app faster and smoother.
useCallback optimization in React Native
const memoizedFunction = useCallback(() => {
// function code here
}, [dependencies]);The function inside useCallback is only recreated if one of the dependencies changes.
Dependencies are values your function uses from outside its own scope.
const increment = useCallback(() => {
setCount(count + 1);
}, [count]);const sayHello = useCallback(() => {
console.log('Hello!');
}, []);const toggle = useCallback(() => {
setOn(prev => !prev);
}, []);This app shows a number and a button. When you press the button, the number goes up by one. The increment function is remembered and not recreated every time the screen updates, making the app smoother.
import React, { useState, useCallback } from 'react'; import { View, Button, Text } from 'react-native'; export default function Counter() { const [count, setCount] = useState(0); const increment = useCallback(() => { setCount(c => c + 1); }, []); return ( <View style={{ padding: 20 }}> <Text style={{ fontSize: 24, marginBottom: 10 }}>Count: {count}</Text> <Button title="Increment" onPress={increment} /> </View> ); }
Remember to list all variables your function uses inside the dependency array.
If you leave dependencies empty, your function won't update when those variables change, which can cause bugs.
useCallback is mainly for optimization; don't use it everywhere unnecessarily.
useCallback keeps functions from being recreated on every screen update.
Use it when passing functions to child components or in hooks to improve performance.
Always include dependencies your function uses to keep it updated correctly.
Practice
useCallback in React Native?Solution
Step 1: Understand function recreation in React
Functions inside components are recreated on every render by default, which can cause unnecessary work.Step 2: Role of useCallback
useCallbackmemoizes the function, so it keeps the same reference unless dependencies change.Final Answer:
To prevent functions from being recreated on every render -> Option BQuick Check:
useCallback = prevent function recreation [OK]
- Thinking useCallback creates state
- Confusing useCallback with styling
- Assuming useCallback fetches data
useCallback?Solution
Step 1: Check useCallback parameters
useCallback takes a function and an array of dependencies as parameters.Step 2: Validate correct syntax
const memoizedFn = useCallback(() => { doSomething(); }, [dependency]); correctly passes an arrow function and an array of dependencies.Final Answer:
const memoizedFn = useCallback(() => { doSomething(); }, [dependency]); -> Option CQuick Check:
useCallback syntax = function + [dependencies] [OK]
- Omitting dependency array
- Passing dependencies without array brackets
- Passing function reference without wrapping in arrow function
const [count, setCount] = useState(0); const increment = useCallback(() => setCount(count + 1), []); increment(); increment(); console.log(count);
Solution
Step 1: Analyze useCallback dependencies
The callback usescountbut has an empty dependency array, so it captures the initial value 0.Step 2: Effect of calling increment()
Callingincrement()callssetCount(0 + 1)twice, but React batches updates and count remains 0 in this synchronous context.Final Answer:
0 -> Option DQuick Check:
Empty dependencies cause stale count = 0 [OK]
- Assuming count updates immediately
- Ignoring stale closure from empty deps
- Expecting increment to add twice
const [value, setValue] = useState(0);
const updateValue = useCallback(() => {
setValue(value + 1);
}, []);Solution
Step 1: Check dependencies in useCallback
The callback usesvaluebut the dependency array is empty, causing stale closure.Step 2: Fix by adding dependency
Addvalueto the dependency array to update the callback whenvaluechanges.Final Answer:
Missing dependency 'value' in useCallback array; add [value] -> Option AQuick Check:
Dependencies must include all used variables [OK]
- Leaving dependency array empty
- Removing useCallback instead of fixing deps
- Misplacing setValue call
useCallback to avoid unnecessary re-renders of the child?Solution
Step 1: Understand callback dependencies
The callback depends on multiple state variables, so it must update when any of them change.Step 2: Use useCallback with all dependencies
Include all dependent state variables in the dependency array to keep the callback updated and stable.Step 3: Avoid empty dependency array
Leaving it empty causes stale values and bugs; defining inline causes new function each render.Final Answer:
Wrap the callback with useCallback including all dependent state variables in the dependency array -> Option AQuick Check:
All dependencies in useCallback deps = stable, updated callback [OK]
- Leaving deps empty causing stale closures
- Defining callback inline causing re-renders
- Confusing useMemo with useCallback
Ready to prove it?
Pass the test and this pattern moves unknown
