BeginnerCoder

Lesson 19 — useCallback

Keep the same function between renders

Each render creates brand-new functions, so a child wrapped in memo (which skips re-rendering when its props are unchanged) still re-renders — the function prop looks "new" every time. useCallback(fn, [deps]) returns the same function object until a dependency changes, so memo can do its job.

const save = useCallback(() => api.save(id), [id]);
<SaveButton onSave={save} />

Why it matters: in big lists and dashboards this stops hundreds of needless re-renders.

Want it another way?
Task

Wrap inc in useCallback with []. Clicking Theme should no longer increase the "+1" button's render count.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with React Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.