Oscar Fanelli
Oscar Fanelli

Reputation: 3657

Empty shouldComponentUpdate works, otherwise not?

I really can't figured out why if I declare an empty shouldComponentUpdate in a component, like this:

shouldComponentUpdate: function(nextProps, nextState) {},

React automatically doesn't render the component when it's not necessary (and that's perfectly fine).

WHILE if I remove the empty declaration, it will render it every time...

I'm using Immutable.js for props.

Upvotes: 0

Views: 361

Answers (2)

J. Mark Stevens
J. Mark Stevens

Reputation: 4945

Oscar is correct, you are getting an undefined. Try

shouldComponentUpdate: function(nextProps, nextState) { return true; },

Upvotes: 0

gcedo
gcedo

Reputation: 4931

From the docs

By default, shouldComponentUpdate always returns true to prevent subtle bugs when state is mutated in place [...]

If you override it with an empty function, it will return undefined, which is cast to false, therefore your component never re-renders (except in the case where a forceUpdate will force it to render without checking shouldComponentUpdate).

Upvotes: 4

Related Questions