05 / 14

Describe pure components.

A React component can be considered pure if it renders the same output for the same state and props. They are the components that do not re-render when the value of props and state has been updated with the same values thus they improve performance.

  1. 1

    Pure components have some performance improvements and render optimizations since React implements the shouldComponentUpdate() method for them with a shallow comparison for props and state.

  2. 2

    For class components like this, React provides the PureComponent base class. Class components that extend the React.PureComponent classes are treated as pure components.

  3. 3

    To create a pure functional component in React, React provides a React.memo() API. Using the React.memo() API, the React functional component can be wrapped as follows to get the React Pure Functional Component.

Since most modern React is functional, we use the memo Higher-Order Component (HOC) to achieve the same result.
Let's talk about some of the features of react pure component
  1. 1

    They prevent the re-rendering if props and state are the same by a Shallow comparison of state and props

  2. 2

    They handle shouldComponentUpdate implicitly

  3. 3

    Increases performance.

The Shallow checks: Because React uses a shallow comparison (using the Object.is algorithm), it only checks the memory references of objects and arrays, not their internal values.
  1. 1

    Primitives: If props.count changes from 5 to 5, it won't re-render. (Correct)

  2. 2

    Objects/Arrays: If you pass a new array literal [1, 2, 3] every time the parent renders, the Pure Component will re-render because the memory reference is different, even if the numbers inside are the same.

Difficulty: 5/10
Topics: shouldComponentUpdate, memoization, performance optimization

Scenario Questions

0-2 years experience
  1. 1

    If you wrap a functional component with React.memo, what effect does that have on its re‑render behavior?

  2. 2

    You notice a component re‑renders even though its props haven't changed. How would you use a pure component to stop the extra renders?

  3. 3

    What happens if you pass a newly created object literal as a prop to a PureComponent on each render?

2-5 years experience
  1. 1

    We have a list component that receives an array of items as a prop. After switching it to a PureComponent, the UI sometimes doesn't update when items are added. Why might that be, and how would you fix it?

  2. 2

    A teammate changed a class component to extend PureComponent and we saw a child component stop receiving updated context. Explain why this could happen.

  3. 3

    When would you choose React.memo over extending PureComponent, and what trade‑offs does each bring in a mixed codebase?

5-8 years experience
  1. 1

    Our dashboard renders thousands of rows. We plan to convert many row components to PureComponent, but we also need dynamic theming. How do you ensure PureComponent doesn't block required updates?

  2. 2

    We are refactoring a legacy codebase that mixes class, functional, and PureComponent. What strategy would you use to decide which components to convert and how would you measure the impact?

  3. 3

    Explain how using PureComponent interacts with Redux's connect and selector memoization. What pitfalls could arise at scale?

8+ years experience
  1. 1

    A large monorepo contains hundreds of UI components, some pure and some not. Design a migration plan to standardize on pure components where beneficial while minimizing risk to downstream teams.

  2. 2

    Consider a cross‑team component library that must support both server‑side rendering and client‑side hydration. How does PureComponent affect SSR consistency, and what policies would you set to avoid hydration mismatches?

  3. 3

    When evaluating long‑term maintainability of a component library, how would you decide whether to expose a PureComponent API versus a regular component with internal memoization, considering versioning and backward compatibility?

Follow-up Questions

  • Can you give an example of a prop that would break the shallow comparison?
  • How does React.memo differ from PureComponent in default behavior?
  • What are the risks of using PureComponent with mutable data structures?