callum
callum

Reputation: 37709

Is state the only difference between a PureComponent and a stateless functional component?

If I define a PureComponent that

...is it effectively identical to a stateless functional component? Or are there any differences in behaviour or performance?

This is not a duplicate of React functional stateless component, PureComponent, Component; what are the differences and when should we use what? because the answer to my question is not contained there, at least not in a way that's easy to pinpoint. That is a big, wide-ranging question, and mine is very specific.

Upvotes: 0

Views: 137

Answers (1)

Sulthan
Sulthan

Reputation: 130092

A stateless functional component is effectively identical to React.Component without lifecycle methods and state, not to React.PureComponent.

The whole point of React.PureComponent is to use one of the life cycle methods (shouldComponentUpdate) and make it to return true only if properties & state have changed, using shallow comparison.

There is no way how to do that in stateless functional components since they are always rendered and have no way to define shouldComponentUpdate.

This behavior is described in detail in Optimizing Performance.

Upvotes: 1

Related Questions