Skip to content

Commit 1f4c337

Browse files
committed
Factoring nits
1 parent eaac7a5 commit 1f4c337

File tree

1 file changed

+59
-90
lines changed

1 file changed

+59
-90
lines changed

packages/react-reconciler/src/ReactFiberHooks.js

Lines changed: 59 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ export function resetHooks(): void {
468468
numberOfReRenders = 0;
469469
}
470470

471-
function createHook(): Hook {
471+
function mountWorkInProgressHook(): Hook {
472472
const hook: Hook = {
473473
memoizedState: null,
474474

@@ -493,42 +493,12 @@ function createHook(): Hook {
493493
);
494494
}
495495
}
496-
497-
return hook;
498-
}
499-
500-
function cloneHook(hook: Hook): Hook {
501-
const nextHook: Hook = {
502-
memoizedState: hook.memoizedState,
503-
504-
baseState: hook.baseState,
505-
queue: hook.queue,
506-
baseUpdate: hook.baseUpdate,
507-
508-
next: null,
509-
};
510-
511-
if (__DEV__) {
512-
nextHook._debugType = ((currentHookNameInDev: any): HookType);
513-
if (currentHookMismatchInDev === null) {
514-
if (currentHookNameInDev !== ((hook: any): HookDev)._debugType) {
515-
currentHookMismatchInDev = new Error('tracer').stack
516-
.split('\n')
517-
.slice(4)
518-
.join('\n');
519-
}
520-
}
521-
}
522-
return nextHook;
523-
}
524-
525-
function mountWorkInProgressHook(): Hook {
526496
if (workInProgressHook === null) {
527497
// This is the first hook in the list
528-
firstWorkInProgressHook = workInProgressHook = createHook();
498+
firstWorkInProgressHook = workInProgressHook = hook;
529499
} else {
530500
// Append to the end of the list
531-
workInProgressHook = workInProgressHook.next = createHook();
501+
workInProgressHook = workInProgressHook.next = hook;
532502
}
533503
return workInProgressHook;
534504
}
@@ -560,12 +530,35 @@ function updateWorkInProgressHook(): Hook {
560530
'file an issue.',
561531
);
562532
currentHook = nextCurrentHook;
533+
534+
const newHook: Hook = {
535+
memoizedState: currentHook.memoizedState,
536+
537+
baseState: currentHook.baseState,
538+
queue: currentHook.queue,
539+
baseUpdate: currentHook.baseUpdate,
540+
541+
next: null,
542+
};
543+
544+
if (__DEV__) {
545+
newHook._debugType = ((currentHookNameInDev: any): HookType);
546+
if (currentHookMismatchInDev === null) {
547+
if (currentHookNameInDev !== ((currentHook: any): HookDev)._debugType) {
548+
currentHookMismatchInDev = new Error('tracer').stack
549+
.split('\n')
550+
.slice(4)
551+
.join('\n');
552+
}
553+
}
554+
}
555+
563556
if (workInProgressHook === null) {
564557
// This is the first hook in the list.
565-
workInProgressHook = firstWorkInProgressHook = cloneHook(currentHook);
558+
workInProgressHook = firstWorkInProgressHook = newHook;
566559
} else {
567560
// Append to the end of the list.
568-
workInProgressHook = workInProgressHook.next = cloneHook(currentHook);
561+
workInProgressHook = workInProgressHook.next = newHook;
569562
}
570563
nextCurrentHook = currentHook.next;
571564
if (nextCurrentHook === null) {
@@ -610,18 +603,17 @@ function updateContext<T>(
610603
return readContext(context, observedBits);
611604
}
612605

613-
function mountReducerImpl<S, A>(
614-
hook: Hook,
606+
function mountReducer<S, A>(
615607
reducer: (S, A) => S,
616608
initialState: void | S,
617609
initialAction: void | null | A,
618610
): [S, Dispatch<A>] {
619-
if (reducer === basicStateReducer) {
620-
// Special case for `useState`.
621-
if (typeof initialState === 'function') {
622-
initialState = initialState();
623-
}
624-
} else if (initialAction !== undefined && initialAction !== null) {
611+
if (__DEV__) {
612+
currentHookNameInDev = 'useReducer';
613+
}
614+
const hook = mountWorkInProgressHook();
615+
// TODO: Lazy init API will change before release.
616+
if (initialAction !== undefined && initialAction !== null) {
625617
initialState = reducer(initialState, initialAction);
626618
}
627619
hook.memoizedState = hook.baseState = initialState;
@@ -639,18 +631,6 @@ function mountReducerImpl<S, A>(
639631
return [hook.memoizedState, dispatch];
640632
}
641633

642-
function mountReducer<S, A>(
643-
reducer: (S, A) => S,
644-
initialState: void | S,
645-
initialAction: void | null | A,
646-
): [S, Dispatch<A>] {
647-
if (__DEV__) {
648-
currentHookNameInDev = 'useReducer';
649-
}
650-
const hook = mountWorkInProgressHook();
651-
return mountReducerImpl(hook, reducer, initialState, initialAction);
652-
}
653-
654634
function updateReducerImpl<S, A>(
655635
hook: Hook,
656636
reducer: (S, A) => S,
@@ -803,7 +783,23 @@ function mountState<S>(
803783
currentHookNameInDev = 'useState';
804784
}
805785
const hook = mountWorkInProgressHook();
806-
return mountReducerImpl(hook, basicStateReducer, initialState);
786+
// TODO: Lazy init API will change before release.
787+
if (typeof initialState === 'function') {
788+
initialState = initialState();
789+
}
790+
hook.memoizedState = hook.baseState = initialState;
791+
const queue = (hook.queue = {
792+
last: null,
793+
dispatch: null,
794+
eagerReducer: basicStateReducer,
795+
eagerState: initialState,
796+
});
797+
const dispatch: Dispatch<A> = (queue.dispatch = (dispatchAction.bind(
798+
null,
799+
currentlyRenderingFiber,
800+
queue,
801+
): any));
802+
return [hook.memoizedState, dispatch];
807803
}
808804

809805
function updateState<S>(
@@ -863,25 +859,15 @@ function updateRef<T>(initialValue: T): {current: T} {
863859
return hook.memoizedState;
864860
}
865861

866-
function mountEffectImpl(
867-
hook,
868-
fiberEffectTag,
869-
hookEffectTag,
870-
create,
871-
deps,
872-
): void {
862+
function mountEffectImpl(fiberEffectTag, hookEffectTag, create, deps): void {
863+
const hook = mountWorkInProgressHook();
873864
const nextDeps = deps === undefined ? null : deps;
874865
sideEffectTag |= fiberEffectTag;
875866
hook.memoizedState = pushEffect(hookEffectTag, create, null, nextDeps);
876867
}
877868

878-
function updateEffectImpl(
879-
hook,
880-
fiberEffectTag,
881-
hookEffectTag,
882-
create,
883-
deps,
884-
): void {
869+
function updateEffectImpl(fiberEffectTag, hookEffectTag, create, deps): void {
870+
const hook = updateWorkInProgressHook();
885871
const nextDeps = deps === undefined ? null : deps;
886872
let destroy = null;
887873

@@ -901,12 +887,7 @@ function updateEffectImpl(
901887
}
902888

903889
sideEffectTag |= fiberEffectTag;
904-
workInProgressHook.memoizedState = pushEffect(
905-
hookEffectTag,
906-
create,
907-
destroy,
908-
nextDeps,
909-
);
890+
hook.memoizedState = pushEffect(hookEffectTag, create, destroy, nextDeps);
910891
}
911892

912893
function mountEffect(
@@ -916,9 +897,7 @@ function mountEffect(
916897
if (__DEV__) {
917898
currentHookNameInDev = 'useEffect';
918899
}
919-
const hook = mountWorkInProgressHook();
920900
return mountEffectImpl(
921-
hook,
922901
UpdateEffect | PassiveEffect,
923902
UnmountPassive | MountPassive,
924903
create,
@@ -933,9 +912,7 @@ function updateEffect(
933912
if (__DEV__) {
934913
currentHookNameInDev = 'useEffect';
935914
}
936-
const hook = updateWorkInProgressHook();
937915
return updateEffectImpl(
938-
hook,
939916
UpdateEffect | PassiveEffect,
940917
UnmountPassive | MountPassive,
941918
create,
@@ -950,9 +927,7 @@ function mountLayoutEffect(
950927
if (__DEV__) {
951928
currentHookNameInDev = 'useLayoutEffect';
952929
}
953-
const hook = mountWorkInProgressHook();
954930
return mountEffectImpl(
955-
hook,
956931
UpdateEffect,
957932
UnmountMutation | MountLayout,
958933
create,
@@ -967,9 +942,7 @@ function updateLayoutEffect(
967942
if (__DEV__) {
968943
currentHookNameInDev = 'useLayoutEffect';
969944
}
970-
const hook = updateWorkInProgressHook();
971945
return updateEffectImpl(
972-
hook,
973946
UpdateEffect,
974947
UnmountMutation | MountLayout,
975948
create,
@@ -1015,14 +988,12 @@ function mountImperativeHandle(
1015988
create !== null ? typeof create : 'null',
1016989
);
1017990
}
1018-
const hook = mountWorkInProgressHook();
1019991

1020992
// TODO: If deps are provided, should we skip comparing the ref itself?
1021993
const effectDeps =
1022994
deps !== null && deps !== undefined ? deps.concat([ref]) : [ref];
1023995

1024996
return mountEffectImpl(
1025-
hook,
1026997
UpdateEffect,
1027998
UnmountMutation | MountLayout,
1028999
imperativeHandleEffect.bind(null, create, ref),
@@ -1044,14 +1015,12 @@ function updateImperativeHandle(
10441015
create !== null ? typeof create : 'null',
10451016
);
10461017
}
1047-
const hook = updateWorkInProgressHook();
10481018

10491019
// TODO: If deps are provided, should we skip comparing the ref itself?
10501020
const effectDeps =
10511021
deps !== null && deps !== undefined ? deps.concat([ref]) : [ref];
10521022

10531023
return updateEffectImpl(
1054-
hook,
10551024
UpdateEffect,
10561025
UnmountMutation | MountLayout,
10571026
imperativeHandleEffect.bind(null, create, ref),
@@ -1286,7 +1255,7 @@ const HooksDispatcherOnMount: Dispatcher = {
12861255
readContext,
12871256

12881257
useCallback: mountCallback,
1289-
useContext: __DEV__ ? mountContext : readContext,
1258+
useContext: readContext,
12901259
useEffect: mountEffect,
12911260
useImperativeHandle: mountImperativeHandle,
12921261
useLayoutEffect: mountLayoutEffect,
@@ -1301,7 +1270,7 @@ const HooksDispatcherOnUpdate: Dispatcher = {
13011270
readContext,
13021271

13031272
useCallback: updateCallback,
1304-
useContext: __DEV__ ? updateContext : readContext,
1273+
useContext: readContext,
13051274
useEffect: updateEffect,
13061275
useImperativeHandle: updateImperativeHandle,
13071276
useLayoutEffect: updateLayoutEffect,

0 commit comments

Comments
 (0)