1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  *
    
  7.  * @flow
    
  8.  */
    
  9. 
    
  10. import {emptyContextObject} from './ReactFizzContext';
    
  11. import {readContext} from './ReactFizzNewContext';
    
  12. 
    
  13. import {disableLegacyContext} from 'shared/ReactFeatureFlags';
    
  14. import {get as getInstance, set as setInstance} from 'shared/ReactInstanceMap';
    
  15. import getComponentNameFromType from 'shared/getComponentNameFromType';
    
  16. import {REACT_CONTEXT_TYPE, REACT_PROVIDER_TYPE} from 'shared/ReactSymbols';
    
  17. import assign from 'shared/assign';
    
  18. import isArray from 'shared/isArray';
    
  19. 
    
  20. const didWarnAboutNoopUpdateForComponent: {[string]: boolean} = {};
    
  21. const didWarnAboutDeprecatedWillMount: {[string]: boolean} = {};
    
  22. 
    
  23. let didWarnAboutUninitializedState;
    
  24. let didWarnAboutGetSnapshotBeforeUpdateWithoutDidUpdate;
    
  25. let didWarnAboutLegacyLifecyclesAndDerivedState;
    
  26. let didWarnAboutUndefinedDerivedState;
    
  27. let didWarnAboutDirectlyAssigningPropsToState;
    
  28. let didWarnAboutContextTypeAndContextTypes;
    
  29. let didWarnAboutInvalidateContextType;
    
  30. let didWarnOnInvalidCallback;
    
  31. 
    
  32. if (__DEV__) {
    
  33.   didWarnAboutUninitializedState = new Set<string>();
    
  34.   didWarnAboutGetSnapshotBeforeUpdateWithoutDidUpdate = new Set<mixed>();
    
  35.   didWarnAboutLegacyLifecyclesAndDerivedState = new Set<string>();
    
  36.   didWarnAboutDirectlyAssigningPropsToState = new Set<string>();
    
  37.   didWarnAboutUndefinedDerivedState = new Set<string>();
    
  38.   didWarnAboutContextTypeAndContextTypes = new Set<mixed>();
    
  39.   didWarnAboutInvalidateContextType = new Set<mixed>();
    
  40.   didWarnOnInvalidCallback = new Set<string>();
    
  41. }
    
  42. 
    
  43. function warnOnInvalidCallback(callback: mixed, callerName: string) {
    
  44.   if (__DEV__) {
    
  45.     if (callback === null || typeof callback === 'function') {
    
  46.       return;
    
  47.     }
    
  48.     const key = callerName + '_' + (callback: any);
    
  49.     if (!didWarnOnInvalidCallback.has(key)) {
    
  50.       didWarnOnInvalidCallback.add(key);
    
  51.       console.error(
    
  52.         '%s(...): Expected the last optional `callback` argument to be a ' +
    
  53.           'function. Instead received: %s.',
    
  54.         callerName,
    
  55.         callback,
    
  56.       );
    
  57.     }
    
  58.   }
    
  59. }
    
  60. 
    
  61. function warnOnUndefinedDerivedState(type: any, partialState: any) {
    
  62.   if (__DEV__) {
    
  63.     if (partialState === undefined) {
    
  64.       const componentName = getComponentNameFromType(type) || 'Component';
    
  65.       if (!didWarnAboutUndefinedDerivedState.has(componentName)) {
    
  66.         didWarnAboutUndefinedDerivedState.add(componentName);
    
  67.         console.error(
    
  68.           '%s.getDerivedStateFromProps(): A valid state object (or null) must be returned. ' +
    
  69.             'You have returned undefined.',
    
  70.           componentName,
    
  71.         );
    
  72.       }
    
  73.     }
    
  74.   }
    
  75. }
    
  76. 
    
  77. function warnNoop(
    
  78.   publicInstance: React$Component<any, any>,
    
  79.   callerName: string,
    
  80. ) {
    
  81.   if (__DEV__) {
    
  82.     const constructor = publicInstance.constructor;
    
  83.     const componentName =
    
  84.       (constructor && getComponentNameFromType(constructor)) || 'ReactClass';
    
  85.     const warningKey = componentName + '.' + callerName;
    
  86.     if (didWarnAboutNoopUpdateForComponent[warningKey]) {
    
  87.       return;
    
  88.     }
    
  89. 
    
  90.     console.error(
    
  91.       '%s(...): Can only update a mounting component. ' +
    
  92.         'This usually means you called %s() outside componentWillMount() on the server. ' +
    
  93.         'This is a no-op.\n\nPlease check the code for the %s component.',
    
  94.       callerName,
    
  95.       callerName,
    
  96.       componentName,
    
  97.     );
    
  98.     didWarnAboutNoopUpdateForComponent[warningKey] = true;
    
  99.   }
    
  100. }
    
  101. 
    
  102. type InternalInstance = {
    
  103.   queue: null | Array<Object>,
    
  104.   replace: boolean,
    
  105. };
    
  106. 
    
  107. const classComponentUpdater = {
    
  108.   isMounted(inst: any) {
    
  109.     return false;
    
  110.   },
    
  111.   // $FlowFixMe[missing-local-annot]
    
  112.   enqueueSetState(inst: any, payload: any, callback) {
    
  113.     const internals: InternalInstance = getInstance(inst);
    
  114.     if (internals.queue === null) {
    
  115.       warnNoop(inst, 'setState');
    
  116.     } else {
    
  117.       internals.queue.push(payload);
    
  118.       if (__DEV__) {
    
  119.         if (callback !== undefined && callback !== null) {
    
  120.           warnOnInvalidCallback(callback, 'setState');
    
  121.         }
    
  122.       }
    
  123.     }
    
  124.   },
    
  125.   enqueueReplaceState(inst: any, payload: any, callback: null) {
    
  126.     const internals: InternalInstance = getInstance(inst);
    
  127.     internals.replace = true;
    
  128.     internals.queue = [payload];
    
  129.     if (__DEV__) {
    
  130.       if (callback !== undefined && callback !== null) {
    
  131.         warnOnInvalidCallback(callback, 'setState');
    
  132.       }
    
  133.     }
    
  134.   },
    
  135.   // $FlowFixMe[missing-local-annot]
    
  136.   enqueueForceUpdate(inst: any, callback) {
    
  137.     const internals: InternalInstance = getInstance(inst);
    
  138.     if (internals.queue === null) {
    
  139.       warnNoop(inst, 'forceUpdate');
    
  140.     } else {
    
  141.       if (__DEV__) {
    
  142.         if (callback !== undefined && callback !== null) {
    
  143.           warnOnInvalidCallback(callback, 'setState');
    
  144.         }
    
  145.       }
    
  146.     }
    
  147.   },
    
  148. };
    
  149. 
    
  150. function applyDerivedStateFromProps(
    
  151.   instance: any,
    
  152.   ctor: any,
    
  153.   getDerivedStateFromProps: (props: any, state: any) => any,
    
  154.   prevState: any,
    
  155.   nextProps: any,
    
  156. ) {
    
  157.   const partialState = getDerivedStateFromProps(nextProps, prevState);
    
  158. 
    
  159.   if (__DEV__) {
    
  160.     warnOnUndefinedDerivedState(ctor, partialState);
    
  161.   }
    
  162.   // Merge the partial state and the previous state.
    
  163.   const newState =
    
  164.     partialState === null || partialState === undefined
    
  165.       ? prevState
    
  166.       : assign({}, prevState, partialState);
    
  167.   return newState;
    
  168. }
    
  169. 
    
  170. export function constructClassInstance(
    
  171.   ctor: any,
    
  172.   props: any,
    
  173.   maskedLegacyContext: any,
    
  174. ): any {
    
  175.   let context = emptyContextObject;
    
  176.   const contextType = ctor.contextType;
    
  177. 
    
  178.   if (__DEV__) {
    
  179.     if ('contextType' in ctor) {
    
  180.       const isValid =
    
  181.         // Allow null for conditional declaration
    
  182.         contextType === null ||
    
  183.         (contextType !== undefined &&
    
  184.           contextType.$$typeof === REACT_CONTEXT_TYPE &&
    
  185.           contextType._context === undefined); // Not a <Context.Consumer>
    
  186. 
    
  187.       if (!isValid && !didWarnAboutInvalidateContextType.has(ctor)) {
    
  188.         didWarnAboutInvalidateContextType.add(ctor);
    
  189. 
    
  190.         let addendum = '';
    
  191.         if (contextType === undefined) {
    
  192.           addendum =
    
  193.             ' However, it is set to undefined. ' +
    
  194.             'This can be caused by a typo or by mixing up named and default imports. ' +
    
  195.             'This can also happen due to a circular dependency, so ' +
    
  196.             'try moving the createContext() call to a separate file.';
    
  197.         } else if (typeof contextType !== 'object') {
    
  198.           addendum = ' However, it is set to a ' + typeof contextType + '.';
    
  199.         } else if (contextType.$$typeof === REACT_PROVIDER_TYPE) {
    
  200.           addendum = ' Did you accidentally pass the Context.Provider instead?';
    
  201.         } else if (contextType._context !== undefined) {
    
  202.           // <Context.Consumer>
    
  203.           addendum = ' Did you accidentally pass the Context.Consumer instead?';
    
  204.         } else {
    
  205.           addendum =
    
  206.             ' However, it is set to an object with keys {' +
    
  207.             Object.keys(contextType).join(', ') +
    
  208.             '}.';
    
  209.         }
    
  210.         console.error(
    
  211.           '%s defines an invalid contextType. ' +
    
  212.             'contextType should point to the Context object returned by React.createContext().%s',
    
  213.           getComponentNameFromType(ctor) || 'Component',
    
  214.           addendum,
    
  215.         );
    
  216.       }
    
  217.     }
    
  218.   }
    
  219. 
    
  220.   if (typeof contextType === 'object' && contextType !== null) {
    
  221.     context = readContext((contextType: any));
    
  222.   } else if (!disableLegacyContext) {
    
  223.     context = maskedLegacyContext;
    
  224.   }
    
  225. 
    
  226.   const instance = new ctor(props, context);
    
  227. 
    
  228.   if (__DEV__) {
    
  229.     if (
    
  230.       typeof ctor.getDerivedStateFromProps === 'function' &&
    
  231.       (instance.state === null || instance.state === undefined)
    
  232.     ) {
    
  233.       const componentName = getComponentNameFromType(ctor) || 'Component';
    
  234.       if (!didWarnAboutUninitializedState.has(componentName)) {
    
  235.         didWarnAboutUninitializedState.add(componentName);
    
  236.         console.error(
    
  237.           '`%s` uses `getDerivedStateFromProps` but its initial state is ' +
    
  238.             '%s. This is not recommended. Instead, define the initial state by ' +
    
  239.             'assigning an object to `this.state` in the constructor of `%s`. ' +
    
  240.             'This ensures that `getDerivedStateFromProps` arguments have a consistent shape.',
    
  241.           componentName,
    
  242.           instance.state === null ? 'null' : 'undefined',
    
  243.           componentName,
    
  244.         );
    
  245.       }
    
  246.     }
    
  247. 
    
  248.     // If new component APIs are defined, "unsafe" lifecycles won't be called.
    
  249.     // Warn about these lifecycles if they are present.
    
  250.     // Don't warn about react-lifecycles-compat polyfilled methods though.
    
  251.     if (
    
  252.       typeof ctor.getDerivedStateFromProps === 'function' ||
    
  253.       typeof instance.getSnapshotBeforeUpdate === 'function'
    
  254.     ) {
    
  255.       let foundWillMountName = null;
    
  256.       let foundWillReceivePropsName = null;
    
  257.       let foundWillUpdateName = null;
    
  258.       if (
    
  259.         typeof instance.componentWillMount === 'function' &&
    
  260.         instance.componentWillMount.__suppressDeprecationWarning !== true
    
  261.       ) {
    
  262.         foundWillMountName = 'componentWillMount';
    
  263.       } else if (typeof instance.UNSAFE_componentWillMount === 'function') {
    
  264.         foundWillMountName = 'UNSAFE_componentWillMount';
    
  265.       }
    
  266.       if (
    
  267.         typeof instance.componentWillReceiveProps === 'function' &&
    
  268.         instance.componentWillReceiveProps.__suppressDeprecationWarning !== true
    
  269.       ) {
    
  270.         foundWillReceivePropsName = 'componentWillReceiveProps';
    
  271.       } else if (
    
  272.         typeof instance.UNSAFE_componentWillReceiveProps === 'function'
    
  273.       ) {
    
  274.         foundWillReceivePropsName = 'UNSAFE_componentWillReceiveProps';
    
  275.       }
    
  276.       if (
    
  277.         typeof instance.componentWillUpdate === 'function' &&
    
  278.         instance.componentWillUpdate.__suppressDeprecationWarning !== true
    
  279.       ) {
    
  280.         foundWillUpdateName = 'componentWillUpdate';
    
  281.       } else if (typeof instance.UNSAFE_componentWillUpdate === 'function') {
    
  282.         foundWillUpdateName = 'UNSAFE_componentWillUpdate';
    
  283.       }
    
  284.       if (
    
  285.         foundWillMountName !== null ||
    
  286.         foundWillReceivePropsName !== null ||
    
  287.         foundWillUpdateName !== null
    
  288.       ) {
    
  289.         const componentName = getComponentNameFromType(ctor) || 'Component';
    
  290.         const newApiName =
    
  291.           typeof ctor.getDerivedStateFromProps === 'function'
    
  292.             ? 'getDerivedStateFromProps()'
    
  293.             : 'getSnapshotBeforeUpdate()';
    
  294.         if (!didWarnAboutLegacyLifecyclesAndDerivedState.has(componentName)) {
    
  295.           didWarnAboutLegacyLifecyclesAndDerivedState.add(componentName);
    
  296.           console.error(
    
  297.             'Unsafe legacy lifecycles will not be called for components using new component APIs.\n\n' +
    
  298.               '%s uses %s but also contains the following legacy lifecycles:%s%s%s\n\n' +
    
  299.               'The above lifecycles should be removed. Learn more about this warning here:\n' +
    
  300.               'https://reactjs.org/link/unsafe-component-lifecycles',
    
  301.             componentName,
    
  302.             newApiName,
    
  303.             foundWillMountName !== null ? `\n  ${foundWillMountName}` : '',
    
  304.             foundWillReceivePropsName !== null
    
  305.               ? `\n  ${foundWillReceivePropsName}`
    
  306.               : '',
    
  307.             foundWillUpdateName !== null ? `\n  ${foundWillUpdateName}` : '',
    
  308.           );
    
  309.         }
    
  310.       }
    
  311.     }
    
  312.   }
    
  313. 
    
  314.   return instance;
    
  315. }
    
  316. 
    
  317. function checkClassInstance(instance: any, ctor: any, newProps: any) {
    
  318.   if (__DEV__) {
    
  319.     const name = getComponentNameFromType(ctor) || 'Component';
    
  320.     const renderPresent = instance.render;
    
  321. 
    
  322.     if (!renderPresent) {
    
  323.       if (ctor.prototype && typeof ctor.prototype.render === 'function') {
    
  324.         console.error(
    
  325.           '%s(...): No `render` method found on the returned component ' +
    
  326.             'instance: did you accidentally return an object from the constructor?',
    
  327.           name,
    
  328.         );
    
  329.       } else {
    
  330.         console.error(
    
  331.           '%s(...): No `render` method found on the returned component ' +
    
  332.             'instance: you may have forgotten to define `render`.',
    
  333.           name,
    
  334.         );
    
  335.       }
    
  336.     }
    
  337. 
    
  338.     if (
    
  339.       instance.getInitialState &&
    
  340.       !instance.getInitialState.isReactClassApproved &&
    
  341.       !instance.state
    
  342.     ) {
    
  343.       console.error(
    
  344.         'getInitialState was defined on %s, a plain JavaScript class. ' +
    
  345.           'This is only supported for classes created using React.createClass. ' +
    
  346.           'Did you mean to define a state property instead?',
    
  347.         name,
    
  348.       );
    
  349.     }
    
  350.     if (
    
  351.       instance.getDefaultProps &&
    
  352.       !instance.getDefaultProps.isReactClassApproved
    
  353.     ) {
    
  354.       console.error(
    
  355.         'getDefaultProps was defined on %s, a plain JavaScript class. ' +
    
  356.           'This is only supported for classes created using React.createClass. ' +
    
  357.           'Use a static property to define defaultProps instead.',
    
  358.         name,
    
  359.       );
    
  360.     }
    
  361.     if (instance.propTypes) {
    
  362.       console.error(
    
  363.         'propTypes was defined as an instance property on %s. Use a static ' +
    
  364.           'property to define propTypes instead.',
    
  365.         name,
    
  366.       );
    
  367.     }
    
  368.     if (instance.contextType) {
    
  369.       console.error(
    
  370.         'contextType was defined as an instance property on %s. Use a static ' +
    
  371.           'property to define contextType instead.',
    
  372.         name,
    
  373.       );
    
  374.     }
    
  375. 
    
  376.     if (disableLegacyContext) {
    
  377.       if (ctor.childContextTypes) {
    
  378.         console.error(
    
  379.           '%s uses the legacy childContextTypes API which is no longer supported. ' +
    
  380.             'Use React.createContext() instead.',
    
  381.           name,
    
  382.         );
    
  383.       }
    
  384.       if (ctor.contextTypes) {
    
  385.         console.error(
    
  386.           '%s uses the legacy contextTypes API which is no longer supported. ' +
    
  387.             'Use React.createContext() with static contextType instead.',
    
  388.           name,
    
  389.         );
    
  390.       }
    
  391.     } else {
    
  392.       if (instance.contextTypes) {
    
  393.         console.error(
    
  394.           'contextTypes was defined as an instance property on %s. Use a static ' +
    
  395.             'property to define contextTypes instead.',
    
  396.           name,
    
  397.         );
    
  398.       }
    
  399. 
    
  400.       if (
    
  401.         ctor.contextType &&
    
  402.         ctor.contextTypes &&
    
  403.         !didWarnAboutContextTypeAndContextTypes.has(ctor)
    
  404.       ) {
    
  405.         didWarnAboutContextTypeAndContextTypes.add(ctor);
    
  406.         console.error(
    
  407.           '%s declares both contextTypes and contextType static properties. ' +
    
  408.             'The legacy contextTypes property will be ignored.',
    
  409.           name,
    
  410.         );
    
  411.       }
    
  412.     }
    
  413. 
    
  414.     if (typeof instance.componentShouldUpdate === 'function') {
    
  415.       console.error(
    
  416.         '%s has a method called ' +
    
  417.           'componentShouldUpdate(). Did you mean shouldComponentUpdate()? ' +
    
  418.           'The name is phrased as a question because the function is ' +
    
  419.           'expected to return a value.',
    
  420.         name,
    
  421.       );
    
  422.     }
    
  423.     if (
    
  424.       ctor.prototype &&
    
  425.       ctor.prototype.isPureReactComponent &&
    
  426.       typeof instance.shouldComponentUpdate !== 'undefined'
    
  427.     ) {
    
  428.       console.error(
    
  429.         '%s has a method called shouldComponentUpdate(). ' +
    
  430.           'shouldComponentUpdate should not be used when extending React.PureComponent. ' +
    
  431.           'Please extend React.Component if shouldComponentUpdate is used.',
    
  432.         getComponentNameFromType(ctor) || 'A pure component',
    
  433.       );
    
  434.     }
    
  435.     if (typeof instance.componentDidUnmount === 'function') {
    
  436.       console.error(
    
  437.         '%s has a method called ' +
    
  438.           'componentDidUnmount(). But there is no such lifecycle method. ' +
    
  439.           'Did you mean componentWillUnmount()?',
    
  440.         name,
    
  441.       );
    
  442.     }
    
  443.     if (typeof instance.componentDidReceiveProps === 'function') {
    
  444.       console.error(
    
  445.         '%s has a method called ' +
    
  446.           'componentDidReceiveProps(). But there is no such lifecycle method. ' +
    
  447.           'If you meant to update the state in response to changing props, ' +
    
  448.           'use componentWillReceiveProps(). If you meant to fetch data or ' +
    
  449.           'run side-effects or mutations after React has updated the UI, use componentDidUpdate().',
    
  450.         name,
    
  451.       );
    
  452.     }
    
  453.     if (typeof instance.componentWillRecieveProps === 'function') {
    
  454.       console.error(
    
  455.         '%s has a method called ' +
    
  456.           'componentWillRecieveProps(). Did you mean componentWillReceiveProps()?',
    
  457.         name,
    
  458.       );
    
  459.     }
    
  460.     if (typeof instance.UNSAFE_componentWillRecieveProps === 'function') {
    
  461.       console.error(
    
  462.         '%s has a method called ' +
    
  463.           'UNSAFE_componentWillRecieveProps(). Did you mean UNSAFE_componentWillReceiveProps()?',
    
  464.         name,
    
  465.       );
    
  466.     }
    
  467.     const hasMutatedProps = instance.props !== newProps;
    
  468.     if (instance.props !== undefined && hasMutatedProps) {
    
  469.       console.error(
    
  470.         '%s(...): When calling super() in `%s`, make sure to pass ' +
    
  471.           "up the same props that your component's constructor was passed.",
    
  472.         name,
    
  473.         name,
    
  474.       );
    
  475.     }
    
  476.     if (instance.defaultProps) {
    
  477.       console.error(
    
  478.         'Setting defaultProps as an instance property on %s is not supported and will be ignored.' +
    
  479.           ' Instead, define defaultProps as a static property on %s.',
    
  480.         name,
    
  481.         name,
    
  482.       );
    
  483.     }
    
  484. 
    
  485.     if (
    
  486.       typeof instance.getSnapshotBeforeUpdate === 'function' &&
    
  487.       typeof instance.componentDidUpdate !== 'function' &&
    
  488.       !didWarnAboutGetSnapshotBeforeUpdateWithoutDidUpdate.has(ctor)
    
  489.     ) {
    
  490.       didWarnAboutGetSnapshotBeforeUpdateWithoutDidUpdate.add(ctor);
    
  491.       console.error(
    
  492.         '%s: getSnapshotBeforeUpdate() should be used with componentDidUpdate(). ' +
    
  493.           'This component defines getSnapshotBeforeUpdate() only.',
    
  494.         getComponentNameFromType(ctor),
    
  495.       );
    
  496.     }
    
  497. 
    
  498.     if (typeof instance.getDerivedStateFromProps === 'function') {
    
  499.       console.error(
    
  500.         '%s: getDerivedStateFromProps() is defined as an instance method ' +
    
  501.           'and will be ignored. Instead, declare it as a static method.',
    
  502.         name,
    
  503.       );
    
  504.     }
    
  505.     if (typeof instance.getDerivedStateFromError === 'function') {
    
  506.       console.error(
    
  507.         '%s: getDerivedStateFromError() is defined as an instance method ' +
    
  508.           'and will be ignored. Instead, declare it as a static method.',
    
  509.         name,
    
  510.       );
    
  511.     }
    
  512.     if (typeof ctor.getSnapshotBeforeUpdate === 'function') {
    
  513.       console.error(
    
  514.         '%s: getSnapshotBeforeUpdate() is defined as a static method ' +
    
  515.           'and will be ignored. Instead, declare it as an instance method.',
    
  516.         name,
    
  517.       );
    
  518.     }
    
  519.     const state = instance.state;
    
  520.     if (state && (typeof state !== 'object' || isArray(state))) {
    
  521.       console.error('%s.state: must be set to an object or null', name);
    
  522.     }
    
  523.     if (
    
  524.       typeof instance.getChildContext === 'function' &&
    
  525.       typeof ctor.childContextTypes !== 'object'
    
  526.     ) {
    
  527.       console.error(
    
  528.         '%s.getChildContext(): childContextTypes must be defined in order to ' +
    
  529.           'use getChildContext().',
    
  530.         name,
    
  531.       );
    
  532.     }
    
  533.   }
    
  534. }
    
  535. 
    
  536. function callComponentWillMount(type: any, instance: any) {
    
  537.   const oldState = instance.state;
    
  538. 
    
  539.   if (typeof instance.componentWillMount === 'function') {
    
  540.     if (__DEV__) {
    
  541.       if (instance.componentWillMount.__suppressDeprecationWarning !== true) {
    
  542.         const componentName = getComponentNameFromType(type) || 'Unknown';
    
  543. 
    
  544.         if (!didWarnAboutDeprecatedWillMount[componentName]) {
    
  545.           console.warn(
    
  546.             // keep this warning in sync with ReactStrictModeWarning.js
    
  547.             'componentWillMount has been renamed, and is not recommended for use. ' +
    
  548.               'See https://reactjs.org/link/unsafe-component-lifecycles for details.\n\n' +
    
  549.               '* Move code from componentWillMount to componentDidMount (preferred in most cases) ' +
    
  550.               'or the constructor.\n' +
    
  551.               '\nPlease update the following components: %s',
    
  552.             componentName,
    
  553.           );
    
  554.           didWarnAboutDeprecatedWillMount[componentName] = true;
    
  555.         }
    
  556.       }
    
  557.     }
    
  558. 
    
  559.     instance.componentWillMount();
    
  560.   }
    
  561.   if (typeof instance.UNSAFE_componentWillMount === 'function') {
    
  562.     instance.UNSAFE_componentWillMount();
    
  563.   }
    
  564. 
    
  565.   if (oldState !== instance.state) {
    
  566.     if (__DEV__) {
    
  567.       console.error(
    
  568.         '%s.componentWillMount(): Assigning directly to this.state is ' +
    
  569.           "deprecated (except inside a component's " +
    
  570.           'constructor). Use setState instead.',
    
  571.         getComponentNameFromType(type) || 'Component',
    
  572.       );
    
  573.     }
    
  574.     classComponentUpdater.enqueueReplaceState(instance, instance.state, null);
    
  575.   }
    
  576. }
    
  577. 
    
  578. function processUpdateQueue(
    
  579.   internalInstance: InternalInstance,
    
  580.   inst: any,
    
  581.   props: any,
    
  582.   maskedLegacyContext: any,
    
  583. ): void {
    
  584.   if (internalInstance.queue !== null && internalInstance.queue.length > 0) {
    
  585.     const oldQueue = internalInstance.queue;
    
  586.     const oldReplace = internalInstance.replace;
    
  587.     internalInstance.queue = null;
    
  588.     internalInstance.replace = false;
    
  589. 
    
  590.     if (oldReplace && oldQueue.length === 1) {
    
  591.       inst.state = oldQueue[0];
    
  592.     } else {
    
  593.       let nextState = oldReplace ? oldQueue[0] : inst.state;
    
  594.       let dontMutate = true;
    
  595.       for (let i = oldReplace ? 1 : 0; i < oldQueue.length; i++) {
    
  596.         const partial = oldQueue[i];
    
  597.         const partialState =
    
  598.           typeof partial === 'function'
    
  599.             ? partial.call(inst, nextState, props, maskedLegacyContext)
    
  600.             : partial;
    
  601.         if (partialState != null) {
    
  602.           if (dontMutate) {
    
  603.             dontMutate = false;
    
  604.             nextState = assign({}, nextState, partialState);
    
  605.           } else {
    
  606.             assign(nextState, partialState);
    
  607.           }
    
  608.         }
    
  609.       }
    
  610.       inst.state = nextState;
    
  611.     }
    
  612.   } else {
    
  613.     internalInstance.queue = null;
    
  614.   }
    
  615. }
    
  616. 
    
  617. // Invokes the mount life-cycles on a previously never rendered instance.
    
  618. export function mountClassInstance(
    
  619.   instance: any,
    
  620.   ctor: any,
    
  621.   newProps: any,
    
  622.   maskedLegacyContext: any,
    
  623. ): void {
    
  624.   if (__DEV__) {
    
  625.     checkClassInstance(instance, ctor, newProps);
    
  626.   }
    
  627. 
    
  628.   const initialState = instance.state !== undefined ? instance.state : null;
    
  629. 
    
  630.   instance.updater = classComponentUpdater;
    
  631.   instance.props = newProps;
    
  632.   instance.state = initialState;
    
  633.   // We don't bother initializing the refs object on the server, since we're not going to resolve them anyway.
    
  634. 
    
  635.   // The internal instance will be used to manage updates that happen during this mount.
    
  636.   const internalInstance: InternalInstance = {
    
  637.     queue: [],
    
  638.     replace: false,
    
  639.   };
    
  640.   setInstance(instance, internalInstance);
    
  641. 
    
  642.   const contextType = ctor.contextType;
    
  643.   if (typeof contextType === 'object' && contextType !== null) {
    
  644.     instance.context = readContext(contextType);
    
  645.   } else if (disableLegacyContext) {
    
  646.     instance.context = emptyContextObject;
    
  647.   } else {
    
  648.     instance.context = maskedLegacyContext;
    
  649.   }
    
  650. 
    
  651.   if (__DEV__) {
    
  652.     if (instance.state === newProps) {
    
  653.       const componentName = getComponentNameFromType(ctor) || 'Component';
    
  654.       if (!didWarnAboutDirectlyAssigningPropsToState.has(componentName)) {
    
  655.         didWarnAboutDirectlyAssigningPropsToState.add(componentName);
    
  656.         console.error(
    
  657.           '%s: It is not recommended to assign props directly to state ' +
    
  658.             "because updates to props won't be reflected in state. " +
    
  659.             'In most cases, it is better to use props directly.',
    
  660.           componentName,
    
  661.         );
    
  662.       }
    
  663.     }
    
  664.   }
    
  665. 
    
  666.   const getDerivedStateFromProps = ctor.getDerivedStateFromProps;
    
  667.   if (typeof getDerivedStateFromProps === 'function') {
    
  668.     instance.state = applyDerivedStateFromProps(
    
  669.       instance,
    
  670.       ctor,
    
  671.       getDerivedStateFromProps,
    
  672.       initialState,
    
  673.       newProps,
    
  674.     );
    
  675.   }
    
  676. 
    
  677.   // In order to support react-lifecycles-compat polyfilled components,
    
  678.   // Unsafe lifecycles should not be invoked for components using the new APIs.
    
  679.   if (
    
  680.     typeof ctor.getDerivedStateFromProps !== 'function' &&
    
  681.     typeof instance.getSnapshotBeforeUpdate !== 'function' &&
    
  682.     (typeof instance.UNSAFE_componentWillMount === 'function' ||
    
  683.       typeof instance.componentWillMount === 'function')
    
  684.   ) {
    
  685.     callComponentWillMount(ctor, instance);
    
  686.     // If we had additional state updates during this life-cycle, let's
    
  687.     // process them now.
    
  688.     processUpdateQueue(
    
  689.       internalInstance,
    
  690.       instance,
    
  691.       newProps,
    
  692.       maskedLegacyContext,
    
  693.     );
    
  694.   }
    
  695. }