@@ -468,7 +468,7 @@ export function resetHooks(): void {
468
468
numberOfReRenders = 0 ;
469
469
}
470
470
471
- function createHook ( ) : Hook {
471
+ function mountWorkInProgressHook ( ) : Hook {
472
472
const hook : Hook = {
473
473
memoizedState : null ,
474
474
@@ -493,42 +493,12 @@ function createHook(): Hook {
493
493
) ;
494
494
}
495
495
}
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 {
526
496
if ( workInProgressHook === null ) {
527
497
// This is the first hook in the list
528
- firstWorkInProgressHook = workInProgressHook = createHook ( ) ;
498
+ firstWorkInProgressHook = workInProgressHook = hook ;
529
499
} else {
530
500
// Append to the end of the list
531
- workInProgressHook = workInProgressHook . next = createHook ( ) ;
501
+ workInProgressHook = workInProgressHook . next = hook ;
532
502
}
533
503
return workInProgressHook ;
534
504
}
@@ -560,12 +530,35 @@ function updateWorkInProgressHook(): Hook {
560
530
'file an issue.' ,
561
531
) ;
562
532
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
+
563
556
if ( workInProgressHook === null ) {
564
557
// This is the first hook in the list.
565
- workInProgressHook = firstWorkInProgressHook = cloneHook ( currentHook ) ;
558
+ workInProgressHook = firstWorkInProgressHook = newHook ;
566
559
} else {
567
560
// Append to the end of the list.
568
- workInProgressHook = workInProgressHook . next = cloneHook ( currentHook ) ;
561
+ workInProgressHook = workInProgressHook . next = newHook ;
569
562
}
570
563
nextCurrentHook = currentHook . next ;
571
564
if ( nextCurrentHook === null ) {
@@ -610,18 +603,17 @@ function updateContext<T>(
610
603
return readContext(context, observedBits);
611
604
}
612
605
613
- function mountReducerImpl < S , A > (
614
- hook: Hook,
606
+ function mountReducer < S , A > (
615
607
reducer: (S, A) => S ,
616
608
initialState : void | S ,
617
609
initialAction : void | null | A ,
618
610
) : [ 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 ) {
625
617
initialState = reducer ( initialState , initialAction ) ;
626
618
}
627
619
hook.memoizedState = hook.baseState = initialState;
@@ -639,18 +631,6 @@ function mountReducerImpl<S, A>(
639
631
return [hook.memoizedState, dispatch];
640
632
}
641
633
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
-
654
634
function updateReducerImpl < S , A > (
655
635
hook: Hook,
656
636
reducer: (S, A) => S ,
@@ -803,7 +783,23 @@ function mountState<S>(
803
783
currentHookNameInDev = 'useState' ;
804
784
}
805
785
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];
807
803
}
808
804
809
805
function updateState < S > (
@@ -863,25 +859,15 @@ function updateRef<T>(initialValue: T): {current: T} {
863
859
return hook.memoizedState;
864
860
}
865
861
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 ( ) ;
873
864
const nextDeps = deps === undefined ? null : deps ;
874
865
sideEffectTag |= fiberEffectTag ;
875
866
hook . memoizedState = pushEffect ( hookEffectTag , create , null , nextDeps ) ;
876
867
}
877
868
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 ( ) ;
885
871
const nextDeps = deps === undefined ? null : deps ;
886
872
let destroy = null ;
887
873
@@ -901,12 +887,7 @@ function updateEffectImpl(
901
887
}
902
888
903
889
sideEffectTag |= fiberEffectTag ;
904
- workInProgressHook . memoizedState = pushEffect (
905
- hookEffectTag ,
906
- create ,
907
- destroy ,
908
- nextDeps ,
909
- ) ;
890
+ hook . memoizedState = pushEffect ( hookEffectTag , create , destroy , nextDeps ) ;
910
891
}
911
892
912
893
function mountEffect (
@@ -916,9 +897,7 @@ function mountEffect(
916
897
if ( __DEV__ ) {
917
898
currentHookNameInDev = 'useEffect' ;
918
899
}
919
- const hook = mountWorkInProgressHook ( ) ;
920
900
return mountEffectImpl (
921
- hook ,
922
901
UpdateEffect | PassiveEffect ,
923
902
UnmountPassive | MountPassive ,
924
903
create,
@@ -933,9 +912,7 @@ function updateEffect(
933
912
if ( __DEV__ ) {
934
913
currentHookNameInDev = 'useEffect' ;
935
914
}
936
- const hook = updateWorkInProgressHook ( ) ;
937
915
return updateEffectImpl (
938
- hook ,
939
916
UpdateEffect | PassiveEffect ,
940
917
UnmountPassive | MountPassive ,
941
918
create,
@@ -950,9 +927,7 @@ function mountLayoutEffect(
950
927
if ( __DEV__ ) {
951
928
currentHookNameInDev = 'useLayoutEffect' ;
952
929
}
953
- const hook = mountWorkInProgressHook ( ) ;
954
930
return mountEffectImpl (
955
- hook ,
956
931
UpdateEffect ,
957
932
UnmountMutation | MountLayout ,
958
933
create ,
@@ -967,9 +942,7 @@ function updateLayoutEffect(
967
942
if ( __DEV__ ) {
968
943
currentHookNameInDev = 'useLayoutEffect' ;
969
944
}
970
- const hook = updateWorkInProgressHook ( ) ;
971
945
return updateEffectImpl (
972
- hook ,
973
946
UpdateEffect ,
974
947
UnmountMutation | MountLayout ,
975
948
create ,
@@ -1015,14 +988,12 @@ function mountImperativeHandle(
1015
988
create !== null ? typeof create : 'null' ,
1016
989
) ;
1017
990
}
1018
- const hook = mountWorkInProgressHook();
1019
991
1020
992
// TODO: If deps are provided, should we skip comparing the ref itself?
1021
993
const effectDeps =
1022
994
deps !== null && deps !== undefined ? deps . concat ( [ ref ] ) : [ ref ] ;
1023
995
1024
996
return mountEffectImpl (
1025
- hook ,
1026
997
UpdateEffect ,
1027
998
UnmountMutation | MountLayout ,
1028
999
imperativeHandleEffect . bind ( null , create , ref ) ,
@@ -1044,14 +1015,12 @@ function updateImperativeHandle(
1044
1015
create !== null ? typeof create : 'null' ,
1045
1016
) ;
1046
1017
}
1047
- const hook = updateWorkInProgressHook();
1048
1018
1049
1019
// TODO: If deps are provided, should we skip comparing the ref itself?
1050
1020
const effectDeps =
1051
1021
deps !== null && deps !== undefined ? deps . concat ( [ ref ] ) : [ ref ] ;
1052
1022
1053
1023
return updateEffectImpl (
1054
- hook ,
1055
1024
UpdateEffect ,
1056
1025
UnmountMutation | MountLayout ,
1057
1026
imperativeHandleEffect . bind ( null , create , ref ) ,
@@ -1286,7 +1255,7 @@ const HooksDispatcherOnMount: Dispatcher = {
1286
1255
readContext ,
1287
1256
1288
1257
useCallback : mountCallback ,
1289
- useContext : __DEV__ ? mountContext : readContext ,
1258
+ useContext : readContext ,
1290
1259
useEffect : mountEffect ,
1291
1260
useImperativeHandle : mountImperativeHandle ,
1292
1261
useLayoutEffect : mountLayoutEffect ,
@@ -1301,7 +1270,7 @@ const HooksDispatcherOnUpdate: Dispatcher = {
1301
1270
readContext ,
1302
1271
1303
1272
useCallback : updateCallback ,
1304
- useContext : __DEV__ ? updateContext : readContext ,
1273
+ useContext : readContext ,
1305
1274
useEffect : updateEffect ,
1306
1275
useImperativeHandle : updateImperativeHandle ,
1307
1276
useLayoutEffect : updateLayoutEffect ,
0 commit comments