From 108a0b5954f8fa829b10c7e6a568bed86f6e7cc6 Mon Sep 17 00:00:00 2001 From: yenshih Date: Tue, 12 Dec 2017 01:10:14 +0800 Subject: [PATCH 1/2] Make warning about state assignment better in `ReactFiberClassComponent` --- .../src/ReactFiberClassComponent.js | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/packages/react-reconciler/src/ReactFiberClassComponent.js b/packages/react-reconciler/src/ReactFiberClassComponent.js index 75ddf38169941..327a01dfc3145 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,24 @@ if (__DEV__) { ); }; + warnAboutStateAssignment = function( + workInProgress: Fiber, + lifeCycle: string, + ) { + const componentName = getComponentName(workInProgress) || 'Component'; + if (!didWarnAboutStateAssignmentForComponent[componentName]) { + warning( + false, + '%s.%s(): Assigning directly to this.state is ' + + "deprecated (except inside a component's " + + 'constructor). Use setState instead.', + componentName, + lifeCycle, + ); + didWarnAboutStateAssignmentForComponent[componentName] = true; + } + }; + // 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 +408,7 @@ 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), - ); + warnAboutStateAssignment(workInProgress, 'componentWillMount'); } updater.enqueueReplaceState(instance, instance.state, null); } @@ -419,17 +432,7 @@ export default function( if (instance.state !== oldState) { 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, - ); - didWarnAboutStateAssignmentForComponent[componentName] = true; - } + warnAboutStateAssignment(workInProgress, 'componentWillReceiveProps'); } updater.enqueueReplaceState(instance, instance.state, null); } From ab03756df6362dfb7a8e21fcc70365f7c8ab21e1 Mon Sep 17 00:00:00 2001 From: yenshih Date: Tue, 12 Dec 2017 01:10:14 +0800 Subject: [PATCH 2/2] Make warning about state assignment better in `ReactFiberClassComponent` --- .../src/ReactFiberClassComponent.js | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/packages/react-reconciler/src/ReactFiberClassComponent.js b/packages/react-reconciler/src/ReactFiberClassComponent.js index 327a01dfc3145..91a6ebf8d2578 100644 --- a/packages/react-reconciler/src/ReactFiberClassComponent.js +++ b/packages/react-reconciler/src/ReactFiberClassComponent.js @@ -58,21 +58,17 @@ if (__DEV__) { }; warnAboutStateAssignment = function( - workInProgress: Fiber, + componentName: string, lifeCycle: string, ) { - const componentName = getComponentName(workInProgress) || 'Component'; - if (!didWarnAboutStateAssignmentForComponent[componentName]) { - warning( - false, - '%s.%s(): Assigning directly to this.state is ' + - "deprecated (except inside a component's " + - 'constructor). Use setState instead.', - componentName, - lifeCycle, - ); - didWarnAboutStateAssignmentForComponent[componentName] = true; - } + 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 @@ -408,7 +404,8 @@ export default function( if (oldState !== instance.state) { if (__DEV__) { - warnAboutStateAssignment(workInProgress, 'componentWillMount'); + const componentName = getComponentName(workInProgress) || 'Component'; + warnAboutStateAssignment(componentName, 'componentWillMount'); } updater.enqueueReplaceState(instance, instance.state, null); } @@ -432,7 +429,11 @@ export default function( if (instance.state !== oldState) { if (__DEV__) { - warnAboutStateAssignment(workInProgress, 'componentWillReceiveProps'); + const componentName = getComponentName(workInProgress) || 'Component'; + if (!didWarnAboutStateAssignmentForComponent[componentName]) { + warnAboutStateAssignment(componentName, 'componentWillReceiveProps'); + didWarnAboutStateAssignmentForComponent[componentName] = true; + } } updater.enqueueReplaceState(instance, instance.state, null); }