Skip to content

Commit ef8bd2c

Browse files
author
Brian Vaughn
committed
Refactor hook ordering check to use DEV-only data structure
This enables us to warn about more cases (e.g. useContext, useDebugValue) withou the need to add any overhead to production bundles.
1 parent 3f5cde5 commit ef8bd2c

File tree

5 files changed

+434
-157
lines changed

5 files changed

+434
-157
lines changed

packages/react-debug-tools/src/ReactDebugHooks.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ function useContext<T>(
9393
context: ReactContext<T>,
9494
observedBits: void | number | boolean,
9595
): T {
96-
nextHook();
9796
hookLog.push({
9897
primitive: 'Context',
9998
stackError: new Error(),

packages/react-reconciler/src/ReactFiber.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {SideEffectTag} from 'shared/ReactSideEffectTags';
1515
import type {ExpirationTime} from './ReactFiberExpirationTime';
1616
import type {UpdateQueue} from './ReactUpdateQueue';
1717
import type {ContextDependencyList} from './ReactFiberNewContext';
18+
import type {HookType} from './ReactFiberHooks';
1819

1920
import invariant from 'shared/invariant';
2021
import warningWithoutStack from 'shared/warningWithoutStack';
@@ -204,6 +205,9 @@ export type Fiber = {|
204205
_debugSource?: Source | null,
205206
_debugOwner?: Fiber | null,
206207
_debugIsCurrentlyTiming?: boolean,
208+
209+
// Used to verify that the order of hooks does not change between renders.
210+
_debugHookTypes?: Array<HookType> | null,
207211
|};
208212

209213
let debugCounter;
@@ -285,6 +289,7 @@ function FiberNode(
285289
this._debugSource = null;
286290
this._debugOwner = null;
287291
this._debugIsCurrentlyTiming = false;
292+
this._debugHookTypes = null;
288293
if (!hasBadMapPolyfill && typeof Object.preventExtensions === 'function') {
289294
Object.preventExtensions(this);
290295
}
@@ -370,6 +375,7 @@ export function createWorkInProgress(
370375
workInProgress._debugID = current._debugID;
371376
workInProgress._debugSource = current._debugSource;
372377
workInProgress._debugOwner = current._debugOwner;
378+
workInProgress._debugHookTypes = current._debugHookTypes;
373379
}
374380

375381
workInProgress.alternate = current;
@@ -723,5 +729,6 @@ export function assignFiberPropertiesInDEV(
723729
target._debugSource = source._debugSource;
724730
target._debugOwner = source._debugOwner;
725731
target._debugIsCurrentlyTiming = source._debugIsCurrentlyTiming;
732+
target._debugHookTypes = source._debugHookTypes;
726733
return target;
727734
}

0 commit comments

Comments
 (0)