useCallback Hook का उपयोग
In this page:
const memoizedFunction = useCallback(() => {
// function body
}, [dependency]);
Why Function References Matter
हर बार जब एक component render होता है, इसके अंदर defined कोई भी function एक brand-new function के रूप में फिर से बनता है, even अगर इसका code identical दिखे। यह मायने रखता है जब वह function एक prop के रूप में React.memo में wrapped एक child को pass किया जाता है, क्योंकि एक नई function reference child को यह सोचने पर मजबूर करती है कि इसके props change हो गए।
उदाहरण: Why Function References Matter
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function Parent() {
const [count, setCount] = React.useState(0);
function handleClick() { console.log("Clicked"); } // recreated every render
return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}
ReactDOM.createRoot(document.getElementById('root')).render(<Parent />);
</script>
</body>
</html>
Memoizing a Function with useCallback
useCallback(fn, dependencies) renders के बीच वही function reference return करता है, जब तक listed dependencies change न हुई हों। यह आपको memoized child components को एक genuinely stable function reference pass करने देता है।
उदाहरण: Memoizing a Function with useCallback
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function Button({ onClick, label }) {
return <button onClick={onClick}>{label}</button>;
}
const MemoButton = React.memo(Button);
function Parent() {
const [count, setCount] = React.useState(0);
const handleClick = React.useCallback(() => {
console.log("Stable callback");
}, []);
return <div><p>{count}</p><MemoButton onClick={handleClick} label="Click" /><button onClick={() => setCount(count + 1)}>+1</button></div>;
}
ReactDOM.createRoot(document.getElementById('root')).render(<Parent />);
</script>
</body>
</html>
useCallback vs useMemo
useCallback और useMemo उसी underlying problem को थोड़े अलग तरीकों से solve करते हैं: useMemo एक calculated VALUE cache करता है, जबकि useCallback खुद एक FUNCTION cache करता है। असल में, useCallback(fn, deps) वैसे ही behave करता है जैसे useMemo(() => fn, deps)।
- जब आपको एक computed result cache करना हो तो useMemo उपयोग करें
- जब आपको specifically एक function reference cache करना हो तो useCallback उपयोग करें।
उदाहरण: useCallback vs useMemo
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function Demo() {
const [n, setN] = React.useState(1);
const doubled = React.useMemo(() => n * 2, [n]); // caches a VALUE
const logDoubled = React.useCallback(() => console.log(doubled), [doubled]); // caches a FUNCTION
return <button onClick={logDoubled}>Doubled: {doubled}</button>;
}
ReactDOM.createRoot(document.getElementById('root')).render(<Demo />);
</script>
</body>
</html>
- हर function को default रूप से useCallback में wrap करना, even ऐसे functions को भी जिनकी इसके लिए कोई real performance need नहीं।
- Callback जिन values को close over करता है उन्हें इसके dependency array में शामिल करना भूल जाना, इसे stale data उपयोग करवाते हुए।
- यह उम्मीद करना कि useCallback अकेला एक child component को re-render होने से रोकेगा -- इसका यह effect होने के लिए इसे child पर React.memo के साथ pair होना चाहिए।
- useCallback एक function को memoize करता है ताकि वही function reference renders के बीच reuse हो।
- यह तब useful है जब React.memo में wrapped child components को callbacks pass करें।
- एक stable reference के बिना, हर render एक नया function बनता है, children में memoization defeat करते हुए।
- useMemo की तरह, इसे genuine performance needs के लिए उपयोग किया जाना चाहिए, default रूप से हर जगह नहीं।
React 16.8 से available है, जब Hooks introduce हुए थे।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: