Skip to content

Commit

Permalink
Track which lifecycle method we're in during DEV
Browse files Browse the repository at this point in the history
Use this to detect setState inside of render instead of relying on
the owner.
  • Loading branch information
acdlite committed Feb 22, 2017
1 parent 5ed2773 commit 9a4de8d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/renderers/shared/fiber/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var invariant = require('invariant');

if (__DEV__) {
var ReactDebugCurrentFiber = require('ReactDebugCurrentFiber');
var ReactDebugLifeCycle = require('ReactDebugLifeCycle');
var warning = require('warning');

var warnedAboutStatelessRefs = {};
Expand Down Expand Up @@ -231,7 +232,11 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(

if (__DEV__) {
ReactCurrentOwner.current = workInProgress;
ReactDebugLifeCycle.current = workInProgress;
ReactDebugLifeCycle.phase = 'render';
nextChildren = fn(nextProps, context);
ReactDebugLifeCycle.current = null;
ReactDebugLifeCycle.phase = null;
} else {
nextChildren = fn(nextProps, context);
}
Expand Down Expand Up @@ -280,7 +285,16 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(

// Rerender
ReactCurrentOwner.current = workInProgress;
const nextChildren = instance.render();
let nextChildren;
if (__DEV__) {
ReactDebugLifeCycle.current = workInProgress;
ReactDebugLifeCycle.phase = 'render';
nextChildren = instance.render();
ReactDebugLifeCycle.current = null;
ReactDebugLifeCycle.phase = null;
} else {
nextChildren = instance.render();
}
reconcileChildren(current, workInProgress, nextChildren);
// Memoize props and state using the values we just used to render.
// TODO: Restructure so we never read values from the instance.
Expand Down
5 changes: 4 additions & 1 deletion src/renderers/shared/fiber/ReactFiberScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ if (__DEV__) {
var warning = require('warning');
var ReactFiberInstrumentation = require('ReactFiberInstrumentation');
var ReactDebugCurrentFiber = require('ReactDebugCurrentFiber');
var ReactDebugLifeCycle = require('ReactDebugLifeCycle');
var {
isProcessingChildContext,
onEndProcessingChildContext,
Expand All @@ -112,7 +113,7 @@ if (__DEV__) {
false,
'setState(...): Cannot call setState() inside getChildContext()',
);
} else if (ReactCurrentOwner.current != null) {
} else if (ReactDebugLifeCycle.phase === 'render') {
warning(
false,
'Cannot update during an existing state transition (such as within ' +
Expand Down Expand Up @@ -879,6 +880,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(config : HostConfig<T, P,
ReactCurrentOwner.current = null;
if (__DEV__) {
ReactDebugCurrentFiber.current = null;
ReactDebugLifeCycle.current = null;
ReactDebugLifeCycle.phase = null;
onEndProcessingChildContext();
}
// It is no longer valid because this unit of work failed.
Expand Down
24 changes: 24 additions & 0 deletions src/renderers/shared/fiber/isomorphic/ReactDebugLifeCycle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright 2013-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 ReactDebugLifeCycle
* @flow
*/

'use strict';

import type { Fiber } from 'ReactFiber';

type LifeCyclePhase = 'render';

const ReactDebugLifeCycle = {
current: (null : Fiber | null),
phase: (null : LifeCyclePhase | null),
};

module.exports = ReactDebugLifeCycle;

0 comments on commit 9a4de8d

Please sign in to comment.