Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default implementation for "shouldComponentUpdate" lifecycle method in React components

A custom implementation for the shouldComponentUpdate() method as part of the React component lifecycle is not required.

I understand it's a boolean function that determines whether render() will be called upon changes in component props and state, and there are mixins like PureRenderMixin which implements the shouldComponentUpdate()

If no custom implementation nor mixins are provided. What's the default implementation and behavior?

like image 397
Ling Zhong Avatar asked Nov 01 '15 20:11

Ling Zhong


2 Answers

As of React v0.13 and v0.14 the default implementation equals to null and as per this logic:

    var shouldUpdate =
      this._pendingForceUpdate ||
      !inst.shouldComponentUpdate ||
      inst.shouldComponentUpdate(nextProps, nextState, nextContext);

the component is updated every render cycle (since !inst.shouldComponentUpdate evaluates to true).

like image 117
zerkms Avatar answered Nov 04 '22 03:11

zerkms


Defaults to true.

The default behaviour is to re-render on every state change, and in the vast majority of cases, you should rely on the default behaviour.

shouldComponentUpdate() is invoked before rendering when new props or state are being received. Defaults to true. This method is not called for the initial render or when forceUpdate() is used.

like image 28
MadhuriJain Avatar answered Nov 04 '22 04:11

MadhuriJain