How React memo and useCallback Work Together
Understand JavaScript reference identity and see when a stable callback helps a memoized React child skip rendering.
On this page
How React memo and useCallback Work Together
useCallbacklets React reuse a function reference across renders while its dependencies remain the same. This can matter when a child component or another hook compares that reference with its previous value.
What is a reference?
- In JavaScript, objects, arrays, and functions have reference identity.
- When you compare two objects with
===, JavaScript checks whether the operands refer to the same object. - Two separate objects can contain identical values and still have different identities.
{} === {} // false,不同物件
[] === [] // false,不同陣列
(() => {}) === (() => {}) // false,不同函式
const obj = {};
obj === obj // true,同一個物件參考What does === compare?
=== performs strict equality. For primitive values such as numbers, strings, and booleans, it compares the values. For objects, arrays, and functions, it checks identity without traversing their contents.
Primitive values
```javascript
console.log(1 === 1); // true,值相同
console.log('hi' === 'hi'); // true,值相同
console.log(true === true); // true,值相同
console.log(null === null); // true,值相同
console.log(1 === 2); // false,值不同
console.log('hi' === 'HI'); // false,值不同
```Reference values
```javascript
const obj1 = { a: 1 };
const obj2 = { a: 1 };
const obj3 = obj1;
console.log(obj1 === obj2); // false,不同記憶體位置
console.log(obj1 === obj3); // true,同一個記憶體位置
const arr1 = [1, 2];
const arr2 = [1, 2];
console.log(arr1 === arr2); // false,不同記憶體位置
function fnA() {}
function fnB() {}
console.log(fnA === fnB); // false,不同記憶體位置
```Why references matter in React
A memoized component compares its previous and next props. Likewise, a hook such as useEffect compares its dependency values. In both cases, React uses Object.is for the individual comparisons. If you create a fresh object, array, or function on each render, its identity changes even when its contents or behavior look the same.
What happens when a parent renders?
A small example
Child component
Start with this child, then look at how the parent passes it a callback.
import React from "react";
function Child({ onClick }) {
console.log("🔄 Child render"); // 每次重新 render 都會印這行
return (
<div>
<p>我是 Child 元件</p>
<button onClick={onClick}>Child 的按鈕</button>
</div>
);
}
export default Child;Parent component
Here is the parent and what happens on the initial mount.
function Parent() {
const [n, setN] = useState(0); // 1) 初始化 state: n = 0
const onClick = () => {}; // 2) 建立一個新的函式 F1(記憶體參考 #1)
// 3) React 進入 render 階段,產生 VDOM:
// <button onClick=...> 與 <Child onClick=F1 />
return (
<>
<button onClick={() => setN(n + 1)}>+1</button> {/* 4) 這裡也建立一個新的匿名函式 G1 */}
<Child onClick={onClick} /> {/* 5) 將 onClick=F1 當作 props 傳給 Child */}
</>
);
}
// 6) Diff:Parent 是第一次出現,直接建立真實 DOM 與 Child 的 fiber。
// 7) Commit:把 DOM 插入頁面、綁定事件監聽(button 的 onClick 綁到 G1)。
// 8) Child 會被渲染一次(因為是初次掛載)。Now click the button once.
function Parent() {
// A) 你點按鈕 → 觸發 G1 → 呼叫 setN(n + 1) → 安排一次 re-render
const [n, setN] = useState(0); // B) React 帶著新狀態進來:n 從 0 → 1
const onClick = () => {}; // C) 重新建立一個新的函式 F2(記憶體參考 #2,≠ F1)
// D) 重新 render,產生新的 VDOM:
// <button onClick=...> 與 <Child onClick=F2 />
return (
<>
<button onClick={() => setN(n + 1)}>+1</button> {/* E) 也重新建立新的匿名函式 G2 */}
<Child onClick={onClick} /> {/* F) 這次傳 onClick=F2 給 Child */}
</>
);
}
// G) Diff:React 會把「上一輪 Child 的 props」和「這一輪 Child 的 props」做 === 比較
// - 上一輪:onClick = F1(參考 #1)
// - 這一輪:onClick = F2(參考 #2)
// - F1 === F2 → false (函式參考不同)What changed?
- The parent's state updates and React renders the parent again. The parent creates a new callback and passes it to the child.
- During the commit, React applies the updates. In this example the button's handler changes from G1, which closed over
n = 0, to G2, which closes overn = 1. The child has rendered again too.
Does the child have to render again?
The problem
When a parent renders, an ordinary child renders as part of that work. Wrapping the child in memo lets React compare props, but a fresh callback still looks like a changed prop.
Combine memo with useCallback
Memoize the child
import React, { memo } from "react";
const Child = memo(function Child({ onClick }: { onClick: () => void }) {
console.log("🔄 Child render");
return (
<div>
<p>我是 Child 元件</p>
<button onClick={onClick}>Child 的按鈕</button>
</div>
);
}
export default Child;Reuse the callback in the parent
function Parent() {
const [n, setN] = useState(0);
// 依賴陣列為 [] → 參考固定不變
const onClick = useCallback(() => {
/* 不用到外部變數 → 可穩定不變 */
}, []);
return (
<>
<button onClick={() => setN(n + 1)}>+1</button>
<Child onClick={onClick} />
</>
);
}What does each API do?
memolets React skip rendering a child when its props are unchanged.- Primitive props can compare equal by value. Object, function, and array props compare by identity.
useCallback(fn, deps)lets React return the same function reference while the dependencies stay the same. That makes it possible for the memoized child to see an unchanged callback prop.
Compare the four combinations
Uses memo? | Uses useCallback? | React compares child props? | Callback identity changes? | Child renders again? |
|---|---|---|---|---|
| No | No | No | Yes | Yes |
| No | Yes | No | No | Yes |
| Yes | No | Yes | Yes | Yes |
| Yes | Yes | Yes | No | It can skip the render if the other props also stay the same |
What to remember
Use useCallback when another component or hook relies on the callback's identity. Include the values the callback uses in its dependency array so it can update when they change.
Use memo when a child frequently receives the same props and its rendering work is worth skipping. If a callback prop changes every time, it can defeat that optimization. The two APIs address different parts of the same example.