useCallback is a React Hook used to memorize a function reference.
It prevents unnecessary function recreation between renders when its dependencies haven't changed.
Syntax:
const memoizedFunction = useCallback(() => {
// Function logic
}, [dependencies]);
Simple example:
import React, { useCallback, useState } from 'react';
function App() {
const [count, setCount] = useState(0);
const display = useCallback(() => {
console.log("Hello React");
}, []);
return (
<div>
<h1>Count: {count}</h1>
<button onClick={() => setCount(count + 1)}>
+
</button>
<button onClick={display}>
Display
</button>
</div>
);
}
export default App;
Top comments (0)