MCPcopy
hub / github.com/facebook/react / mountClassInstance

Function mountClassInstance

packages/react-server/src/ReactFizzClassComponent.js:626–703  ·  view source on GitHub ↗
(
  instance: any,
  ctor: any,
  newProps: any,
  maskedLegacyContext: any,
)

Source from the content-addressed store, hash-verified

624
625// Invokes the mount life-cycles on a previously never rendered instance.
626export function mountClassInstance(
627 instance: any,
628 ctor: any,
629 newProps: any,
630 maskedLegacyContext: any,
631): void {
632 if (__DEV__) {
633 checkClassInstance(instance, ctor, newProps);
634 }
635
636 const initialState = instance.state !== undefined ? instance.state : null;
637
638 instance.updater = classComponentUpdater;
639 instance.props = newProps;
640 instance.state = initialState;
641 // We don't bother initializing the refs object on the server, since we're not going to resolve them anyway.
642
643 // The internal instance will be used to manage updates that happen during this mount.
644 const internalInstance: InternalInstance = {
645 queue: [],
646 replace: false,
647 };
648 setInstance(instance, internalInstance);
649
650 const contextType = ctor.contextType;
651 if (typeof contextType === 'object' && contextType !== null) {
652 instance.context = readContext(contextType);
653 } else if (disableLegacyContext) {
654 instance.context = emptyContextObject;
655 } else {
656 instance.context = maskedLegacyContext;
657 }
658
659 if (__DEV__) {
660 if (instance.state === newProps) {
661 const componentName = getComponentNameFromType(ctor) || 'Component';
662 if (!didWarnAboutDirectlyAssigningPropsToState.has(componentName)) {
663 didWarnAboutDirectlyAssigningPropsToState.add(componentName);
664 console.error(
665 '%s: It is not recommended to assign props directly to state ' +
666 "because updates to props won't be reflected in state. " +
667 'In most cases, it is better to use props directly.',
668 componentName,
669 );
670 }
671 }
672 }
673
674 const getDerivedStateFromProps = ctor.getDerivedStateFromProps;
675 if (typeof getDerivedStateFromProps === 'function') {
676 instance.state = applyDerivedStateFromProps(
677 instance,
678 ctor,
679 getDerivedStateFromProps,
680 initialState,
681 newProps,
682 );
683 }

Callers 1

renderClassComponentFunction · 0.90

Calls 9

readContextFunction · 0.90
getComponentNameFromTypeFunction · 0.85
addMethod · 0.80
checkClassInstanceFunction · 0.70
callComponentWillMountFunction · 0.70
processUpdateQueueFunction · 0.70
hasMethod · 0.65
errorMethod · 0.65

Tested by

no test coverage detected