Do I need React.PureComponent when the component is `connect`ed to react-redux store?

Viewed 533

They say we can improve the performance of React app by using React.PureComponent in some cases, because it shallow compares its props before updating the component.

https://reactjs.org/docs/react-api.html#reactpurecomponent

On the other hand, react-redux's mapStateToProps uses shallow comparison when it decides whether we need to update the component.

https://redux.js.org/faq/reactredux#why-is-my-component-re-rendering-too-often

So, can I say I do not need React.PureComponent when its props are from react-redux connect, and it has no state? Or is there any difference between them?

1 Answers

As long as your component receives all its props from Redux and all changes to state can be recognized with a shallow comparison, you can use regular Component to the same effect as PureComponent. Just make sure when you update the state you don't mutate it, but instead return a new copy of the state. Immutable.JS is extremely helpful for this.

Related