Search code examples
javascriptreactjsrecompose

Recompose pure() vs React.PureComponent


What's the difference between pure() from the Recompose library and React.PureComponent? I'm guessing they are essentially solving the same problem. Can someone please clarify this?


Solution

  • The difference is that React.PureComponent is stateful component and keeps track on the state:

    React.PureComponent is similar to React.Component. The difference between them is that React.Component doesn’t implement shouldComponentUpdate(), but React.PureComponent implements it with a shallow prop and state comparison.

    While Recompose is aimed at stateless functional components, pure shallowly detects changes in props only.

    Both use shouldComponentUpdate to shallowly detect changes, so there's no practical difference between them, as long as a component doesn't involve local state.