useCallback with memo

Give a memo child the same function every render with useCallback, and typing no longer re-renders the Save button.

React · intermediate · 7 min · 25 steps · free

What you'll learn

UseCallback.jsx

import { memo, useCallback, useState } from 'react';

const SaveButton = memo(function SaveButton({ onSave }) {
  return <button onClick={onSave}>Save</button>;
});

export default function Editor() {
  const [text, setText] = useState('');
  const [saves, setSaves] = useState(0);
  const handleSave = useCallback(() => setSaves((s) => s + 1), []);

  return (
    <div>
      <input value={text} onChange={(e) => setText(e.target.value)} placeholder="Write here" />
      <SaveButton onSave={handleSave} />
      <p>Saved {saves} times</p>
    </div>
  );
}

Step by step

  1. React renders <Editor> for the first time: it calls the Editor function.
  2. useState runs for the first time, so React stores the starting value for text and returns it: "".
  3. useState runs for the first time, so React stores the starting value for saves and returns it: 0.
  4. useCallback stores the function and returns it.
  5. Editor returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.
  6. React renders <SaveButton> for the first time: it calls the SaveButton function. Its props: onSave = ƒ () => setSaves((s) => s + 1).
  7. SaveButton returns JSX: a description of what the screen should show, <button>Save</button>. React has not changed the page yet.
  8. React turns the JSX into real page elements and shows them: look at the Screen panel.
  9. You type in the box. Only text changes.
  10. setText(e.target.value) does not change text right away: React queues the update and will render again. In this call text is still "".
  11. React renders <Editor> again (render 2): it calls Editor once more, from the top.
  12. useState returns the value React has kept for text: "Hi". The starting value is only used on the first render.
  13. useState returns the value React has kept for saves: 0. The starting value is only used on the first render.
  14. useCallback returns the same function as last time: its dependency list [] has not changed.
  15. Editor returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.
  16. Editor rendered; SaveButton did not. Compare with the version without useCallback.
  17. You click Save.
  18. setSaves((s) => s + 1) queues an update function. React will call it with the latest saves when it renders again.
  19. React renders <Editor> again (render 3): it calls Editor once more, from the top.
  20. useState returns the value React has kept for text: "Hi". The starting value is only used on the first render.
  21. React calls the update function with s = 0. It returns 1, the new value.
  22. useState returns the value React has kept for saves: 1. The starting value is only used on the first render.
  23. useCallback gives back a remembered function for handleSave. It only makes a new one when a dependency changes.
  24. Editor returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.
  25. Saved 1 times.

Common mistakes

Best practices

Key takeaways

Check yourself

useCallback(fn, []) returns?

Answer: The same function every render

useCallback is the same as?

Answer: useMemo(() => fn, deps)

·

More in Performance