@@ -49,10 +49,10 @@ export type Dispatcher = {
49
49
observedBits: void | number | boolean,
50
50
): T,
51
51
useState< S > (initialState: (() => S ) | S ) : [ S , Dispatch < BasicStateAction < S >> ] ,
52
- useReducer < S , A > (
52
+ useReducer < S , I , A > (
53
53
reducer : ( S , A ) = > S ,
54
- initialState : S ,
55
- initialAction : A | void | null ,
54
+ initialArg : I ,
55
+ init ? : ( I ) => S ,
56
56
) : [ S , Dispatch < A > ] ,
57
57
useContext < T > (
58
58
context : ReactContext < T > ,
@@ -591,16 +591,17 @@ function updateContext<T>(
591
591
return readContext(context, observedBits);
592
592
}
593
593
594
- function mountReducer < S , A > (
594
+ function mountReducer < S , I , A > (
595
595
reducer: (S, A) => S ,
596
- initialState : void | S ,
597
- initialAction : void | null | A ,
596
+ initialArg : I ,
597
+ init ?: I => S ,
598
598
) : [ S , Dispatch < A > ] {
599
599
const hook = mountWorkInProgressHook ( ) ;
600
- // TODO: Lazy init API will change before release.
601
- if ( initialAction !== undefined && initialAction !== null ) {
602
- // $FlowFixMe - Must express with overloading.
603
- initialState = reducer ( initialState , initialAction ) ;
600
+ let initialState ;
601
+ if ( init !== undefined ) {
602
+ initialState = init ( initialArg ) ;
603
+ } else {
604
+ initialState = ( ( initialArg : any ) : S ) ;
604
605
}
605
606
hook.memoizedState = hook.baseState = initialState;
606
607
const queue = (hook.queue = {
@@ -618,10 +619,10 @@ function mountReducer<S, A>(
618
619
return [hook.memoizedState, dispatch];
619
620
}
620
621
621
- function updateReducer < S , A > (
622
+ function updateReducer < S , I , A > (
622
623
reducer: (S, A) => S ,
623
- initialState : void | S ,
624
- initialAction : void | null | A ,
624
+ initialArg : I ,
625
+ init ?: I => S ,
625
626
) : [ S , Dispatch < A > ] {
626
627
const hook = updateWorkInProgressHook ( ) ;
627
628
const queue = hook . queue ;
@@ -755,7 +756,6 @@ function mountState<S>(
755
756
initialState: (() => S ) | S ,
756
757
) : [ S , Dispatch < BasicStateAction < S > > ] {
757
758
const hook = mountWorkInProgressHook ( ) ;
758
- // TODO: Lazy init API will change before release.
759
759
if ( typeof initialState === 'function' ) {
760
760
initialState = initialState ( ) ;
761
761
}
@@ -1282,16 +1282,16 @@ if (__DEV__) {
1282
1282
ReactCurrentDispatcher . current = prevDispatcher ;
1283
1283
}
1284
1284
} ,
1285
- useReducer < S , A > (
1285
+ useReducer < S , I , A > (
1286
1286
reducer: (S, A) => S ,
1287
- initialState : S ,
1288
- initialAction : A | void | null ,
1287
+ initialArg : I ,
1288
+ init ?: I => S ,
1289
1289
) : [ S , Dispatch < A > ] {
1290
1290
currentHookNameInDev = 'useReducer' ;
1291
1291
const prevDispatcher = ReactCurrentDispatcher . current ;
1292
1292
ReactCurrentDispatcher . current = InvalidNestedHooksDispatcherOnMountInDEV ;
1293
1293
try {
1294
- return mountReducer ( reducer , initialState , initialAction ) ;
1294
+ return mountReducer ( reducer , initialArg , init ) ;
1295
1295
} finally {
1296
1296
ReactCurrentDispatcher . current = prevDispatcher ;
1297
1297
}
@@ -1366,16 +1366,16 @@ if (__DEV__) {
1366
1366
ReactCurrentDispatcher . current = prevDispatcher ;
1367
1367
}
1368
1368
} ,
1369
- useReducer < S , A > (
1369
+ useReducer < S , I , A > (
1370
1370
reducer: (S, A) => S ,
1371
- initialState : S ,
1372
- initialAction : A | void | null ,
1371
+ initialArg : I ,
1372
+ init ?: I => S ,
1373
1373
) : [ S , Dispatch < A > ] {
1374
1374
currentHookNameInDev = 'useReducer' ;
1375
1375
const prevDispatcher = ReactCurrentDispatcher . current ;
1376
1376
ReactCurrentDispatcher . current = InvalidNestedHooksDispatcherOnUpdateInDEV ;
1377
1377
try {
1378
- return updateReducer ( reducer , initialState , initialAction ) ;
1378
+ return updateReducer ( reducer , initialArg , init ) ;
1379
1379
} finally {
1380
1380
ReactCurrentDispatcher . current = prevDispatcher ;
1381
1381
}
@@ -1457,17 +1457,17 @@ if (__DEV__) {
1457
1457
ReactCurrentDispatcher . current = prevDispatcher ;
1458
1458
}
1459
1459
} ,
1460
- useReducer < S , A > (
1460
+ useReducer < S , I , A > (
1461
1461
reducer: (S, A) => S ,
1462
- initialState : S ,
1463
- initialAction : A | void | null ,
1462
+ initialArg : I ,
1463
+ init ?: I => S ,
1464
1464
) : [ S , Dispatch < A > ] {
1465
1465
currentHookNameInDev = 'useReducer' ;
1466
1466
warnInvalidHookAccess ( ) ;
1467
1467
const prevDispatcher = ReactCurrentDispatcher . current ;
1468
1468
ReactCurrentDispatcher . current = InvalidNestedHooksDispatcherOnMountInDEV ;
1469
1469
try {
1470
- return mountReducer ( reducer , initialState , initialAction ) ;
1470
+ return mountReducer ( reducer , initialArg , init ) ;
1471
1471
} finally {
1472
1472
ReactCurrentDispatcher . current = prevDispatcher ;
1473
1473
}
@@ -1552,17 +1552,17 @@ if (__DEV__) {
1552
1552
ReactCurrentDispatcher . current = prevDispatcher ;
1553
1553
}
1554
1554
} ,
1555
- useReducer < S , A > (
1555
+ useReducer < S , I , A > (
1556
1556
reducer: (S, A) => S ,
1557
- initialState : S ,
1558
- initialAction : A | void | null ,
1557
+ initialArg : I ,
1558
+ init ?: I => S ,
1559
1559
) : [ S , Dispatch < A > ] {
1560
1560
currentHookNameInDev = 'useReducer' ;
1561
1561
warnInvalidHookAccess ( ) ;
1562
1562
const prevDispatcher = ReactCurrentDispatcher . current ;
1563
1563
ReactCurrentDispatcher . current = InvalidNestedHooksDispatcherOnUpdateInDEV ;
1564
1564
try {
1565
- return updateReducer ( reducer , initialState , initialAction ) ;
1565
+ return updateReducer ( reducer , initialArg , init ) ;
1566
1566
} finally {
1567
1567
ReactCurrentDispatcher . current = prevDispatcher ;
1568
1568
}
0 commit comments