Menu

React memo vs useMemo vs useCallback — When to Use Which
📰

React memo vs useMemo vs useCallback — When to Use Which

DEV Community: react·Mohsen Fallahnejad·13 days ago
#F86GrLMh
#devtoreact#devto#dev#memo#usecallback
Reading 0:00
15s threshold

React memo vs useMemo vs useCallback

All three are about skipping unnecessary work . But they work differently.

🧠 The Core Idea

React re-renders a component whenever its parent re-renders — even if nothing changed. Memoization lets you say:

"Only redo this if the inputs actually changed."

1️⃣ React.memo — Skip re-rendering a component

Wraps a component . Skips re-render if props didn't change.

const Button = React . memo (({ onClick , label }: { onClick : () => void ; label : string }) => { console . log ( " Button rendered " ); return < button onClick = { onClick } > { label } </ button >; });

Enter fullscreen mode

Exit fullscreen mode

Without memo : Button re-renders every time the parent does.

With memo : Button skips re-render if onClick and label are the same.

2️⃣ useMemo — Cache a computed value

Memoizes the result of an expensive calculation.

const total = useMemo (() => { return items . reduce (( sum , item ) => sum + item .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.