Skip to content

Proposal: Audit PureComponent usage and potentially stop that #2062

@BPScott

Description

@BPScott

Part of the post-v4 work is to update our examples to use hooks. @AndrewMusgrave made some fantastic progress on this and @amrocha has been reviewing it and he brought up something interesting: Pervasive usage of useCallback() makes them a bit crap and noisy to read.

Killing the useCallbacks would make for easier to read examples but there's a gotcha: The majority of our components use PureComponent and not using useCallback would mean they always fail their memoisation check (as you end up passing in a new function every time) - meaning a mechanism for supposed performance improvement actually becomes harmful as you always do more work with no chance of hitting the short-circuit path.

A component has two choices and we must be consistent if we want to showcase the correct way to use a component:

  • Don't use React.PureComponent/React.memo in the component, and don't use useCallback to wrap functions passed into the component (as there is no shallow compare, it is preferable to avoid the small overhead of memoification)
  • Use React.PureComponent/React.memo in the component and use useCallback to wrap functions passed into the component (as the wrapping allows the memoisation to work)

A lot of our components use PureComponent which make it seem like the second way is the right way to go but I'm not convinced that a most of our usage of PureComponent is actually justified.


A chat with @TheMallen in #web-foundation-tech suggests that their consensus is to avoid using React.PureComponent and React.memo until you know you need them and are sure there is a meaningful performance improvement thanks to profiling information. This approach to profiling and validating before jumping on a path with a different performance tradeoff is echoed by https://kentcdodds.com/blog/usememo-and-usecallback and https://reacttraining.com/blog/react-inline-functions-and-performance/ .

It looks like the overwhelming majority of our PureComponent usage is cargo-culty - we've slapped it onto components without ensuring it actually helps as many of the components in question are leaf nodes that often will have very little complex content inside them (e.g. Avatar, Banner, TextField).

I would like to take some time to audit our usage of PureComponent on all our components to validate our assumption that PureComponent makes a positive difference. If this turns out to be unfounded then we should remove PureComponent on the components that do not benefit from it.

// @cc @chloerice @dleroux @danrosenthal and @tmlayton for our earlier conversations on this

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions