diff --git a/packages/react-reconciler/src/ReactFiberClassComponent.js b/packages/react-reconciler/src/ReactFiberClassComponent.js index 75ddf3816994..91a6ebf8d257 100644 --- a/packages/react-reconciler/src/ReactFiberClassComponent.js +++ b/packages/react-reconciler/src/ReactFiberClassComponent.js @@ -42,6 +42,7 @@ const isArray = Array.isArray; let didWarnAboutStateAssignmentForComponent; let warnOnInvalidCallback; +let warnAboutStateAssignment; if (__DEV__) { didWarnAboutStateAssignmentForComponent = {}; @@ -56,6 +57,20 @@ if (__DEV__) { ); }; + warnAboutStateAssignment = function( + componentName: string, + lifeCycle: string, + ) { + warning( + false, + '%s.%s(): Assigning directly to this.state is ' + + "deprecated (except inside a component's " + + 'constructor). Use setState instead.', + componentName, + lifeCycle, + ); + }; + // This is so gross but it's at least non-critical and can be removed if // it causes problems. This is meant to give a nicer error message for // ReactDOM15.unstable_renderSubtreeIntoContainer(reactDOM16Component, @@ -389,13 +404,8 @@ export default function( if (oldState !== instance.state) { if (__DEV__) { - warning( - false, - '%s.componentWillMount(): Assigning directly to this.state is ' + - "deprecated (except inside a component's " + - 'constructor). Use setState instead.', - getComponentName(workInProgress), - ); + const componentName = getComponentName(workInProgress) || 'Component'; + warnAboutStateAssignment(componentName, 'componentWillMount'); } updater.enqueueReplaceState(instance, instance.state, null); } @@ -421,13 +431,7 @@ export default function( if (__DEV__) { const componentName = getComponentName(workInProgress) || 'Component'; if (!didWarnAboutStateAssignmentForComponent[componentName]) { - warning( - false, - '%s.componentWillReceiveProps(): Assigning directly to ' + - "this.state is deprecated (except inside a component's " + - 'constructor). Use setState instead.', - componentName, - ); + warnAboutStateAssignment(componentName, 'componentWillReceiveProps'); didWarnAboutStateAssignmentForComponent[componentName] = true; } }