Search code examples
reactjslifecycle

Where are lifecycle methods defined in React's source code?


where is shouldComponentUpdate and other lifecycle function defined in react source code? I was digging in the react source code, and simply can't find the definition.. anyone can give a hint?

or to put it this way: where specifically did react decide to return for shouldComponentUpdate?


Solution

  • Lifecycle methods aren't 'defined' in React's code, so to speak - it simply checks if they exist on your component's instances at certain points in their lifecycle, and if so, it runs them.

    For example, here's some of the code from the React reconciler package that checks shouldComponentUpdate:

    if (typeof instance.shouldComponentUpdate === 'function') {
      startPhaseTimer(workInProgress, 'shouldComponentUpdate');
      const shouldUpdate = instance.shouldComponentUpdate(
        newProps,
        newState,
        newContext,
      );
      stopPhaseTimer();
    
      /* ...and so on... */
    
      return shouldUpdate;
    }