useCallback is a React Hook used to memoize functions. In React Native, it helps prevent unnecessary re-creation of functions on every render, which can improve performance when passing callbacks to child components.
useCallback memoizes a function referenceuseEffect
// Basic syntax of useCallback hook
const memoizedCallback = useCallback(() => {
doSomething();
}, [dependency]);
// Prevents function recreation on every render
import React, { useState, useCallback } from "react";
import { View, Text, Button } from "react-native";
export default function App() {
const [count, setCount] = useState(0);
const increment = useCallback(() => {
setCount(prev => prev + 1);
}, []);
return (
<View>
<Text>Count: {count}</Text>
<Button title="Increment" onPress={increment} />
</View>
);
}
The increment function is memoized and will not be recreated on every render. This is useful when passing it to child components wrapped with React.memo.
Use the simulator below to understand how useCallback prevents child components from re-rendering when the parent updates unrelated state.
Parent State (Unrelated): 0
Receives: handleClick prop
useCallback only when neededReact.memo for best resultsReact.memouseCallback and compare behavior