0

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.

Oscar Fanelli
  • 3,337
  • 2
  • 28
  • 40

2 Answers2

4

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).

Ed Ballot
  • 3,405
  • 1
  • 17
  • 24
gcedo
  • 4,811
  • 1
  • 21
  • 28
0

Oscar is correct, you are getting an undefined. Try

shouldComponentUpdate: function(nextProps, nextState) { return true; },
J. Mark Stevens
  • 4,911
  • 2
  • 13
  • 18