Preventing extra renders with React.memo

In the previous lesson, you split State and Dispatch into separate contexts to stop “write-only” components from re-rendering on every tick. In this lesson, you will push that idea into the UI. We will use React.memo to keep static UI pieces from re-rendering when their inputs have not changed. Our focus is the GameActions UI: you will memoize the ActionButton leaf component and also the container itself so parent updates do not cascade.

Memoizing the leaf: ActionButton

What it does:

  • Wraps the button card in React.memo so React can skip re-rendering when its props are the same (shallow compare).
  • Receives three inputs: the action descriptor, the onClick handler, and the disabled flag.
  • Blurs the button after a click for a cleaner keyboard/focus experience.
  • This concentrates re-rendering on the specific buttons whose inputs actually change.
Passing the right props to each memoized button

What it does:

  • Renders a grid of ActionButton components, one per action descriptor.
  • onClick triggers the proper game action (only when enabled).
  • disabled also honors the global pause state to prevent accidental input during a pause.
  • Because each ActionButton gets only the data it needs, React.memo can compare those props and avoid re-rendering buttons whose inputs did not change.
Memoizing the container

What it does:

  • Wraps the GameActions container in React.memo so it will not re-render due to parent updates unless its own props change.
  • Combined with the separate Dispatch context (from the previous lesson), this helps isolate the action panel so the frequent timer tick does not ripple through components that do not need to change.
Summary and what’s next

You learned how to:

  • Use React.memo to keep leaf UI (ActionButton) from re-rendering when props are unchanged.
  • Pass focused props (action, onClick, disabled) so only the affected buttons update.
  • Memoize the container to block parent-driven re-renders.

Up next, you will put these ideas into action and see how much smoother the game feels when only the right components re-render.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal