2

According to the docs, React.Component doesn't implement shouldComponentUpdate ?

So how about a lifecycle of a component (the-component-lifecycle) (shouldcomponentupdate) ?

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.

2
  • What is the question here? Commented Jan 15, 2018 at 22:01
  • @DanielReina As you can see, in one part of the docs, they say that React.Component doesn't implements shouldComponentUpdate function, which seem not to be true, accroding to the other part of the docs. Commented Jan 15, 2018 at 22:05

1 Answer 1

2

By default, using Component, shouldComponentUpdate returns true (no diffing of props happens), unless its defined by you (result dependent of what you put in). Using PureComponent react does a shallow diff of props by default.

Feel free to take a look at the react source code, roughly, it checks if shouldComponentUpdate is defined and is a function. Then checks if it is a pure component, then returns true by default.

https://github.com/facebook/react/blob/master/packages/react-reconciler/src/ReactFiberClassComponent.js

Sign up to request clarification or add additional context in comments.

4 Comments

So, when we're extending Component class, react doesn't perform any checks and always re-renders whole component -_-?
Correct, if you look at the react source code github.com/facebook/react/blob/master/packages/react-reconciler/… line 157 checkShouldComponentUpdate
In every tutorial, first thing to mention is always react shadow DOM and the fact, that react always update only necessary part of the component. So, accoring to everything you wrote above, it's not true? Or maybe i'm missing something?
I see your point, I might be wrong on this one...but just because it should update it doesn't mean it will update

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.