Skip to content

Commit

Permalink
Refactor hook ordering check to use DEV-only data structure
Browse files Browse the repository at this point in the history
This enables us to warn about more cases (e.g. useContext, useDebugValue) withou the need to add any overhead to production bundles.
  • Loading branch information
Brian Vaughn committed Feb 26, 2019
1 parent 842d135 commit 9bbd4f4
Show file tree
Hide file tree
Showing 5 changed files with 434 additions and 157 deletions.
1 change: 0 additions & 1 deletion packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ function useContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
): T {
nextHook();
hookLog.push({
primitive: 'Context',
stackError: new Error(),
Expand Down
7 changes: 7 additions & 0 deletions packages/react-reconciler/src/ReactFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {SideEffectTag} from 'shared/ReactSideEffectTags';
import type {ExpirationTime} from './ReactFiberExpirationTime';
import type {UpdateQueue} from './ReactUpdateQueue';
import type {ContextDependencyList} from './ReactFiberNewContext';
import type {HookType} from './ReactFiberHooks';

import invariant from 'shared/invariant';
import warningWithoutStack from 'shared/warningWithoutStack';
Expand Down Expand Up @@ -204,6 +205,9 @@ export type Fiber = {|
_debugSource?: Source | null,
_debugOwner?: Fiber | null,
_debugIsCurrentlyTiming?: boolean,

// Used to verify that the order of hooks does not change between renders.
_debugHookTypes?: Array<HookType> | null,
|};

let debugCounter;
Expand Down Expand Up @@ -285,6 +289,7 @@ function FiberNode(
this._debugSource = null;
this._debugOwner = null;
this._debugIsCurrentlyTiming = false;
this._debugHookTypes = null;
if (!hasBadMapPolyfill && typeof Object.preventExtensions === 'function') {
Object.preventExtensions(this);
}
Expand Down Expand Up @@ -370,6 +375,7 @@ export function createWorkInProgress(
workInProgress._debugID = current._debugID;
workInProgress._debugSource = current._debugSource;
workInProgress._debugOwner = current._debugOwner;
workInProgress._debugHookTypes = current._debugHookTypes;
}

workInProgress.alternate = current;
Expand Down Expand Up @@ -723,5 +729,6 @@ export function assignFiberPropertiesInDEV(
target._debugSource = source._debugSource;
target._debugOwner = source._debugOwner;
target._debugIsCurrentlyTiming = source._debugIsCurrentlyTiming;
target._debugHookTypes = source._debugHookTypes;
return target;
}
Loading

0 comments on commit 9bbd4f4

Please sign in to comment.