Skip to content

Commit

Permalink
Deduplicated many warnings (#11140)
Browse files Browse the repository at this point in the history
*  Deduplicated the following warnings:

1.  Can only update a mounted or mounting component.
    This usually means you called setState, replaceState,
    or forceUpdate on an unmounted component. This is a no-op

2.  %s.componentWillReceiveProps(): Assigning directly to
    this.state is deprecated (except inside a component's
    constructor). Use setState instead.'

3.  An update (setState, replaceState, or forceUpdate) was scheduled
    from inside an update function. Update functions should be pure,
    with zero side-effects. Consider using componentDidUpdate or a
    callback.

4.  setState(...): Cannot call setState() inside getChildContext()

* Code review changes made for #11140
  • Loading branch information
imanushree committed Oct 28, 2017
1 parent 84a2891 commit 1eba12e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 27 deletions.
8 changes: 2 additions & 6 deletions packages/react-dom/src/client/ReactDOMFiberSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,9 @@ var ReactDOMSelect = {
};

if (__DEV__) {
if (
props.value !== undefined &&
props.defaultValue !== undefined &&
!didWarnValueDefaultValue
) {
if (props.value !== undefined && props.defaultValue !== undefined) {
warning(
false,
didWarnValueDefaultValue,
'Select elements must be either controlled or uncontrolled ' +
'(specify either the value prop, or the defaultValue prop, but not ' +
'both). Decide between using a controlled or uncontrolled select ' +
Expand Down
10 changes: 8 additions & 2 deletions packages/react-dom/src/server/ReactPartialRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ if (__DEV__) {
var {ReactDebugCurrentFrame} = require('shared/ReactGlobalSharedState');
var currentDebugStack = null;
var currentDebugElementStack = null;
var errorInfo = {};
var setCurrentDebugStack = function(stack: Array<Frame>) {
var frame: Frame = stack[stack.length - 1];
currentDebugElementStack = ((frame: any): FrameDev).debugElementStack;
Expand Down Expand Up @@ -175,15 +176,20 @@ function warnNoop(
) {
if (__DEV__) {
var constructor = publicInstance.constructor;
const currentComponent =
(constructor && getComponentName(constructor)) || 'ReactClass';
const currentComponentError = `${callerName}_${currentComponent}`;

warning(
false,
!!errorInfo[currentComponentError],
'%s(...): Can only update a mounting component. ' +
'This usually means you called %s() outside componentWillMount() on the server. ' +
'This is a no-op.\n\nPlease check the code for the %s component.',
callerName,
callerName,
(constructor && getComponentName(constructor)) || 'ReactClass',
currentComponent,
);
errorInfo[currentComponentError] = true;
}
}

Expand Down
8 changes: 6 additions & 2 deletions packages/react-reconciler/src/ReactFiberClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ if (__DEV__) {
var warning = require('fbjs/lib/warning');

var {startPhaseTimer, stopPhaseTimer} = require('./ReactDebugFiberPerf');
var stateDeprecationWarning = {};

var warnOnInvalidCallback = function(callback: mixed, callerName: string) {
warning(
Expand Down Expand Up @@ -393,13 +394,16 @@ module.exports = function(

if (instance.state !== oldState) {
if (__DEV__) {
const currentComponent =
getComponentName(workInProgress) || 'Component';
warning(
false,
!!stateDeprecationWarning[currentComponent],
'%s.componentWillReceiveProps(): Assigning directly to ' +
"this.state is deprecated (except inside a component's " +
'constructor). Use setState instead.',
getComponentName(workInProgress),
currentComponent,
);
stateDeprecationWarning[currentComponent] = true;
}
updater.enqueueReplaceState(instance, instance.state, null);
}
Expand Down
30 changes: 17 additions & 13 deletions packages/react-reconciler/src/ReactFiberScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,41 +101,45 @@ if (__DEV__) {
} = require('./ReactDebugFiberPerf');

var didWarnAboutStateTransition = false;
var didWarnSetStateChildContext = false;
var ownerHasNoopWarning = {};

var warnAboutUpdateOnUnmounted = function(
instance: React$ComponentType<any>,
) {
const ctor = instance.constructor;
const ctor: Object = instance.constructor;
const currentComponent =
(ctor && (ctor.displayName || ctor.name)) || 'ReactClass';

warning(
false,
'Can only update a mounted or mounting component. This usually means ' +
'you called setState, replaceState, or forceUpdate on an unmounted ' +
'component. This is a no-op.\n\nPlease check the code for the ' +
'%s component.',
(ctor && (ctor.displayName || ctor.name)) || 'ReactClass',
!!ownerHasNoopWarning[currentComponent],
'Can only update a mounted or mounting ' +
'component. This usually means you called setState, replaceState, ' +
'or forceUpdate on an unmounted component. This is a no-op.\n\nPlease ' +
'check the code for the %s component.',
currentComponent,
);
ownerHasNoopWarning[currentComponent] = true;
};

var warnAboutInvalidUpdates = function(instance: React$ComponentType<any>) {
switch (ReactDebugCurrentFiber.phase) {
case 'getChildContext':
warning(
false,
didWarnSetStateChildContext,
'setState(...): Cannot call setState() inside getChildContext()',
);
didWarnSetStateChildContext = true;
break;
case 'render':
if (didWarnAboutStateTransition) {
return;
}
didWarnAboutStateTransition = true;
warning(
false,
didWarnAboutStateTransition,
'Cannot update during an existing state transition (such as within ' +
"`render` or another component's constructor). Render methods should " +
'be a pure function of props and state; constructor side-effects are ' +
'an anti-pattern, but can be moved to `componentWillMount`.',
);
didWarnAboutStateTransition = true;
break;
}
};
Expand Down
4 changes: 3 additions & 1 deletion packages/react-reconciler/src/ReactFiberUpdateQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {NoWork} = require('./ReactFiberExpirationTime');

if (__DEV__) {
var warning = require('fbjs/lib/warning');
var didWarnUpdateInsideUpdate = false;
}

type PartialState<State, Props> =
Expand Down Expand Up @@ -134,12 +135,13 @@ function insertUpdateIntoFiber<State>(
if (__DEV__) {
if (queue1.isProcessing || (queue2 !== null && queue2.isProcessing)) {
warning(
false,
didWarnUpdateInsideUpdate,
'An update (setState, replaceState, or forceUpdate) was scheduled ' +
'from inside an update function. Update functions should be pure, ' +
'with zero side-effects. Consider using componentDidUpdate or a ' +
'callback.',
);
didWarnUpdateInsideUpdate = true;
}
}

Expand Down
12 changes: 9 additions & 3 deletions packages/react/src/ReactNoopUpdateQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,27 @@

if (__DEV__) {
var warning = require('fbjs/lib/warning');
var errorInfo = {};
}

function warnNoop(publicInstance, callerName) {
if (__DEV__) {
var constructor = publicInstance.constructor;
const currentComponent =
(constructor && (constructor.displayName || constructor.name)) ||
'ReactClass';
const currentComponentError = `${callerName}_${currentComponent}`;

warning(
false,
!!errorInfo[currentComponentError],
'%s(...): Can only update a mounted or mounting component. ' +
'This usually means you called %s() on an unmounted component. ' +
'This is a no-op.\n\nPlease check the code for the %s component.',
callerName,
callerName,
(constructor && (constructor.displayName || constructor.name)) ||
'ReactClass',
currentComponent,
);
errorInfo[currentComponentError] = true;
}
}

Expand Down

0 comments on commit 1eba12e

Please sign in to comment.