BeginnerCoder

Lesson 18 — useMemo

Skip work that has not changed

Every re-render runs your whole component again, including slow calculations. useMemo(() => calc(), [deps]) remembers (memoizes) the result and only recalculates when something in the dependency array changes. Other state changes reuse the saved value.

const sorted = useMemo(() => [...rows].sort(byName), [rows]);

Why it matters: it keeps typing and clicking smooth when a page filters or sorts big lists.

Want it another way?
Task

Wrap the sum in useMemo with [nums]. Clicking Theme should no longer increase "Sum runs" — only Add number should.

← 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.