React
  • Introduction
  • Getting Started
    • Introduction
    • Before you get started
  • 1. Fundamentals
    • Introduction
    • Rendering
      • JSX
      • Exercise
      • Solution
    • Components
      • Stateless
        • Exercise
        • Solution
      • Stateful
        • Exercise
        • Solution
      • Styling
        • Exercise
        • Solution
        • Using CSS Modules
    • Folder Architecture
  • 2. Intermediate
    • Lifecycle methods
    • Controlled and Uncontrolled components
    • Anti-patterns
    • Refs and the DOM
    • Lifting State Up
  • 3. Advanced Topics
    • Conventions
    • Reconciliation
    • Performance Optimizations
      • Avoiding Reconciliation
      • PureComponent
      • Avoiding inline lambdas
      • Development vs Production build
    • Context
  • 4. Advanced Patterns
    • Higher-order Components
    • Children as Function
    • Renderless Components
    • Portals
    • Error handling
  • Exercises
    • Introduction
    • 1. ProductList light
      • Step 1
      • Step 2
      • Step 3
      • Step 4
      • Extra
      • Solution
Powered by GitBook
On this page
  1. 3. Advanced Topics
  2. Performance Optimizations

PureComponent

PreviousAvoiding ReconciliationNextAvoiding inline lambdas

Last updated 6 years ago

React.PureComponent is exactly like React.Component but implements shouldComponentUpdate() with a shallow prop and state comparison.

If your React component’s render() function renders the same result given the same props and state, you can use React.PureComponent for a performance boost in some cases.

When to use PureComponent vs Stateless (functional) components One question that often comes up is whether to use Stateless (functional) components or PureComponent.

For simple, presentational-only components that need to be easily reused and don’t require state, prefer . The ESLint rule [react/prefer-stateless-function](https://github.com/Shopify/eslint-plugin-shopify/blob/master/lib/config/rules/react.js#L63) currently enforces this pattern when using the preset.

This way you're sure they are decoupled from the actual app logic, that they are dead-easy to test and that they don't have unexpected side effects. The exception is if for some reason you have a lot of them or if you really need to optimise their render method (as you can't define shouldComponentUpdate for a stateless functional component).

One might be tempted to think that functional components would be more performant and have less overhead than class-based components, but this is currently not the case. Internally, stateless component are wrapped in a class and follow the same code path as classed-based components. However, the React team has mentioned that they intend to work on improving the performance of stateless functional components in the future. The original confirms this:

In the future, we’ll also be able to make performance optimizations specific to these components by avoiding unnecessary checks and memory allocations.

stateless functional components
eslint-preset-shopify
React 0.14 release notes