Skip to content

Commit 3694412

Browse files
committed
Add type identifier to every hook
1 parent 71b3a03 commit 3694412

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

packages/react-reconciler/src/ReactFiberHooks.js

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ export type Hook = {
198198
baseQueue: Update<any, any> | null,
199199
queue: any,
200200
next: Hook | null,
201+
type: HookType | null,
201202
};
202203

203204
// The effect "instance" is a shared object that remains the same for the entire
@@ -977,7 +978,7 @@ export function resetHooksOnUnwind(workInProgress: Fiber): void {
977978
thenableState = null;
978979
}
979980

980-
function mountWorkInProgressHook(): Hook {
981+
function mountWorkInProgressHook(type: HookType | null): Hook {
981982
const hook: Hook = {
982983
memoizedState: null,
983984

@@ -986,6 +987,7 @@ function mountWorkInProgressHook(): Hook {
986987
queue: null,
987988

988989
next: null,
990+
type,
989991
};
990992

991993
if (workInProgressHook === null) {
@@ -1259,7 +1261,7 @@ function mountReducer<S, I, A>(
12591261
initialArg: I,
12601262
init?: I => S,
12611263
): [S, Dispatch<A>] {
1262-
const hook = mountWorkInProgressHook();
1264+
const hook = mountWorkInProgressHook('useReducer');
12631265
let initialState;
12641266
if (init !== undefined) {
12651267
initialState = init(initialArg);
@@ -1637,7 +1639,7 @@ function mountSyncExternalStore<T>(
16371639
getServerSnapshot?: () => T,
16381640
): T {
16391641
const fiber = currentlyRenderingFiber;
1640-
const hook = mountWorkInProgressHook();
1642+
const hook = mountWorkInProgressHook('useSyncExternalStore');
16411643

16421644
let nextSnapshot;
16431645
const isHydrating = getIsHydrating();
@@ -1892,8 +1894,11 @@ function forceStoreRerender(fiber: Fiber) {
18921894
}
18931895
}
18941896

1895-
function mountStateImpl<S>(initialState: (() => S) | S): Hook {
1896-
const hook = mountWorkInProgressHook();
1897+
function mountStateImpl<S>(
1898+
initialState: (() => S) | S,
1899+
type: HookType | null,
1900+
): Hook {
1901+
const hook = mountWorkInProgressHook(type);
18971902
if (typeof initialState === 'function') {
18981903
const initialStateInitializer = initialState;
18991904
// $FlowFixMe[incompatible-use]: Flow doesn't like mixed types
@@ -1923,7 +1928,7 @@ function mountStateImpl<S>(initialState: (() => S) | S): Hook {
19231928
function mountState<S>(
19241929
initialState: (() => S) | S,
19251930
): [S, Dispatch<BasicStateAction<S>>] {
1926-
const hook = mountStateImpl(initialState);
1931+
const hook = mountStateImpl(initialState, 'useState');
19271932
const queue = hook.queue;
19281933
const dispatch: Dispatch<BasicStateAction<S>> = (dispatchSetState.bind(
19291934
null,
@@ -1950,7 +1955,7 @@ function mountOptimistic<S, A>(
19501955
passthrough: S,
19511956
reducer: ?(S, A) => S,
19521957
): [S, (A) => void] {
1953-
const hook = mountWorkInProgressHook();
1958+
const hook = mountWorkInProgressHook('useOptimistic');
19541959
hook.memoizedState = hook.baseState = passthrough;
19551960
const queue: UpdateQueue<S, A> = {
19561961
pending: null,
@@ -2377,7 +2382,7 @@ function mountActionState<S, P>(
23772382

23782383
// State hook. The state is stored in a thenable which is then unwrapped by
23792384
// the `use` algorithm during render.
2380-
const stateHook = mountWorkInProgressHook();
2385+
const stateHook = mountWorkInProgressHook('useActionState');
23812386
stateHook.memoizedState = stateHook.baseState = initialState;
23822387
// TODO: Typing this "correctly" results in recursion limit errors
23832388
// const stateQueue: UpdateQueue<S | Awaited<S>, S | Awaited<S>> = {
@@ -2398,7 +2403,10 @@ function mountActionState<S, P>(
23982403

23992404
// Pending state. This is used to store the pending state of the action.
24002405
// Tracked optimistically, like a transition pending state.
2401-
const pendingStateHook = mountStateImpl((false: Thenable<boolean> | boolean));
2406+
const pendingStateHook = mountStateImpl(
2407+
(false: Thenable<boolean> | boolean),
2408+
null,
2409+
);
24022410
const setPendingState: boolean => void = (dispatchOptimisticSetState.bind(
24032411
null,
24042412
currentlyRenderingFiber,
@@ -2413,7 +2421,7 @@ function mountActionState<S, P>(
24132421
// shared between all instances of the hook. Similar to a regular state queue,
24142422
// but different because the actions are run sequentially, and they run in
24152423
// an event instead of during render.
2416-
const actionQueueHook = mountWorkInProgressHook();
2424+
const actionQueueHook = mountWorkInProgressHook(null);
24172425
const actionQueue: ActionStateQueue<S, P> = {
24182426
state: initialState,
24192427
dispatch: (null: any), // circular
@@ -2601,7 +2609,7 @@ function createEffectInstance(): EffectInstance {
26012609
}
26022610

26032611
function mountRef<T>(initialValue: T): {current: T} {
2604-
const hook = mountWorkInProgressHook();
2612+
const hook = mountWorkInProgressHook('useRef');
26052613
const ref = {current: initialValue};
26062614
hook.memoizedState = ref;
26072615
return ref;
@@ -2617,8 +2625,9 @@ function mountEffectImpl(
26172625
hookFlags: HookFlags,
26182626
create: () => (() => void) | void,
26192627
deps: Array<mixed> | void | null,
2628+
type: HookType | null,
26202629
): void {
2621-
const hook = mountWorkInProgressHook();
2630+
const hook = mountWorkInProgressHook(type);
26222631
const nextDeps = deps === undefined ? null : deps;
26232632
currentlyRenderingFiber.flags |= fiberFlags;
26242633
hook.memoizedState = pushSimpleEffect(
@@ -2682,13 +2691,15 @@ function mountEffect(
26822691
HookPassive,
26832692
create,
26842693
deps,
2694+
'useEffect',
26852695
);
26862696
} else {
26872697
mountEffectImpl(
26882698
PassiveEffect | PassiveStaticEffect,
26892699
HookPassive,
26902700
create,
26912701
deps,
2702+
'useEffect',
26922703
);
26932704
}
26942705
}
@@ -2723,7 +2734,7 @@ function useEffectEventImpl<Args, Return, F: (...Array<Args>) => Return>(
27232734
function mountEvent<Args, Return, F: (...Array<Args>) => Return>(
27242735
callback: F,
27252736
): F {
2726-
const hook = mountWorkInProgressHook();
2737+
const hook = mountWorkInProgressHook('useEffectEvent');
27272738
const ref = {impl: callback};
27282739
hook.memoizedState = ref;
27292740
// $FlowIgnore[incompatible-return]
@@ -2758,7 +2769,13 @@ function mountInsertionEffect(
27582769
create: () => (() => void) | void,
27592770
deps: Array<mixed> | void | null,
27602771
): void {
2761-
mountEffectImpl(UpdateEffect, HookInsertion, create, deps);
2772+
mountEffectImpl(
2773+
UpdateEffect,
2774+
HookInsertion,
2775+
create,
2776+
deps,
2777+
'useInsertionEffect',
2778+
);
27622779
}
27632780

27642781
function updateInsertionEffect(
@@ -2779,7 +2796,13 @@ function mountLayoutEffect(
27792796
) {
27802797
fiberFlags |= MountLayoutDevEffect;
27812798
}
2782-
return mountEffectImpl(fiberFlags, HookLayout, create, deps);
2799+
return mountEffectImpl(
2800+
fiberFlags,
2801+
HookLayout,
2802+
create,
2803+
deps,
2804+
'useLayoutEffect',
2805+
);
27832806
}
27842807

27852808
function updateLayoutEffect(
@@ -2855,6 +2878,7 @@ function mountImperativeHandle<T>(
28552878
HookLayout,
28562879
imperativeHandleEffect.bind(null, create, ref),
28572880
effectDeps,
2881+
'useImperativeHandle',
28582882
);
28592883
}
28602884

@@ -2894,7 +2918,7 @@ function mountDebugValue<T>(value: T, formatterFn: ?(value: T) => mixed): void {
28942918
const updateDebugValue = mountDebugValue;
28952919

28962920
function mountCallback<T>(callback: T, deps: Array<mixed> | void | null): T {
2897-
const hook = mountWorkInProgressHook();
2921+
const hook = mountWorkInProgressHook('useCallback');
28982922
const nextDeps = deps === undefined ? null : deps;
28992923
hook.memoizedState = [callback, nextDeps];
29002924
return callback;
@@ -2918,7 +2942,7 @@ function mountMemo<T>(
29182942
nextCreate: () => T,
29192943
deps: Array<mixed> | void | null,
29202944
): T {
2921-
const hook = mountWorkInProgressHook();
2945+
const hook = mountWorkInProgressHook('useMemo');
29222946
const nextDeps = deps === undefined ? null : deps;
29232947
const nextValue = nextCreate();
29242948
if (shouldDoubleInvokeUserFnsInHooksDEV) {
@@ -2961,7 +2985,7 @@ function updateMemo<T>(
29612985
}
29622986

29632987
function mountDeferredValue<T>(value: T, initialValue?: T): T {
2964-
const hook = mountWorkInProgressHook();
2988+
const hook = mountWorkInProgressHook('useDeferredValue');
29652989
return mountDeferredValueImpl(hook, value, initialValue);
29662990
}
29672991

@@ -3399,7 +3423,10 @@ function mountTransition(): [
33993423
boolean,
34003424
(callback: () => void, options?: StartTransitionOptions) => void,
34013425
] {
3402-
const stateHook = mountStateImpl((false: Thenable<boolean> | boolean));
3426+
const stateHook = mountStateImpl(
3427+
(false: Thenable<boolean> | boolean),
3428+
'useTransition',
3429+
);
34033430
// The `start` method never changes.
34043431
const start = startTransition.bind(
34053432
null,
@@ -3408,7 +3435,7 @@ function mountTransition(): [
34083435
true,
34093436
false,
34103437
);
3411-
const hook = mountWorkInProgressHook();
3438+
const hook = mountWorkInProgressHook(null);
34123439
hook.memoizedState = start;
34133440
return [false, start];
34143441
}
@@ -3448,7 +3475,7 @@ function useHostTransitionStatus(): TransitionStatus {
34483475
}
34493476

34503477
function mountId(): string {
3451-
const hook = mountWorkInProgressHook();
3478+
const hook = mountWorkInProgressHook('useId');
34523479

34533480
const root = ((getWorkInProgressRoot(): any): FiberRoot);
34543481
// TODO: In Fizz, id generation is specific to each server config. Maybe we
@@ -3491,7 +3518,7 @@ function updateId(): string {
34913518
}
34923519

34933520
function mountRefresh(): any {
3494-
const hook = mountWorkInProgressHook();
3521+
const hook = mountWorkInProgressHook('useCacheRefresh');
34953522
const refresh = (hook.memoizedState = refreshCache.bind(
34963523
null,
34973524
currentlyRenderingFiber,

0 commit comments

Comments
 (0)