mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Fix #6114 - Calling setState inside getChildContext should warn
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
var ReactInvalidSetStateWarningDevTool = require('ReactInvalidSetStateWarningDevTool');
|
||||
var warning = require('warning');
|
||||
|
||||
var eventHandlers = [];
|
||||
@@ -48,6 +49,15 @@ var ReactDebugTool = {
|
||||
}
|
||||
}
|
||||
},
|
||||
onBeginProcessingChildContext() {
|
||||
emitEvent('onBeginProcessingChildContext');
|
||||
},
|
||||
onEndProcessingChildContext() {
|
||||
emitEvent('onEndProcessingChildContext');
|
||||
},
|
||||
onSetState() {
|
||||
emitEvent('onSetState');
|
||||
},
|
||||
onMountRootComponent(internalInstance) {
|
||||
emitEvent('onMountRootComponent', internalInstance);
|
||||
},
|
||||
@@ -62,4 +72,6 @@ var ReactDebugTool = {
|
||||
},
|
||||
};
|
||||
|
||||
ReactDebugTool.addDevtool(ReactInvalidSetStateWarningDevTool);
|
||||
|
||||
module.exports = ReactDebugTool;
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Copyright 2016-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
* @providesModule ReactInvalidSetStateWarningDevTool
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var warning = require('warning');
|
||||
|
||||
if (__DEV__) {
|
||||
var processingChildContext = false;
|
||||
|
||||
var warnInvalidSetState = function() {
|
||||
warning(
|
||||
!processingChildContext,
|
||||
'setState(...): Cannot call setState() inside getChildContext()'
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
var ReactInvalidSetStateWarningDevTool = {
|
||||
onBeginProcessingChildContext() {
|
||||
processingChildContext = true;
|
||||
},
|
||||
onEndProcessingChildContext() {
|
||||
processingChildContext = false;
|
||||
},
|
||||
onSetState() {
|
||||
warnInvalidSetState();
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = ReactInvalidSetStateWarningDevTool;
|
||||
@@ -12,6 +12,7 @@
|
||||
'use strict';
|
||||
|
||||
var ReactNoopUpdateQueue = require('ReactNoopUpdateQueue');
|
||||
var ReactInstrumentation = require('ReactInstrumentation');
|
||||
|
||||
var canDefineProperty = require('canDefineProperty');
|
||||
var emptyObject = require('emptyObject');
|
||||
@@ -66,6 +67,7 @@ ReactComponent.prototype.setState = function(partialState, callback) {
|
||||
'function which returns an object of state variables.'
|
||||
);
|
||||
if (__DEV__) {
|
||||
ReactInstrumentation.debugTool.onSetState();
|
||||
warning(
|
||||
partialState != null,
|
||||
'setState(...): You passed an undefined or null state object; ' +
|
||||
|
||||
Reference in New Issue
Block a user