# 1620 MINuseMemo vs useCallback vs React.memo
# 16
20 MIN
Overview
Learn to prevent unnecessary renders with useMemo, useCallback, and React.memo in React.
Solution preview
Here's the result you'll build by completing this challenge.
Requirements
- The app should render a counter that increments correctly from two different buttons.
- The expensive operation should only run once, even after multiple renders.
- The result of the expensive operation should remain constant across re-renders.
- One of the buttons (the memoized one) should skip re-rendering when the counter updates.
- The
Buttoncomponent should receive an object prop (id) alongside its callback prop. React.memoalone must not be enough: the object and callback props must also be stabilized (withuseMemoanduseCallback) for the memoized button to skip re-rendering.
Notes
- The normal button and the memoized button should both increment the counter.
- Console logs can help verify that re-renders happen only when needed.
- The expensive operation must not be recalculated after the first render.
React.memoonly shallow-compares props, so inline objects and inline functions still trigger re-renders.- Use
useMemoto keep the object prop stable anduseCallbackto keep the callback prop stable. - Ensure that only components affected by state changes re-render.
Tests
- increments normal count on Button click
- increments count using MemoButton click
- calls expensiveOperation only once
- renders memoized expensive operation result
- memoized button skips re-rendering when the count updates