@@ -49,10 +49,10 @@ export type Dispatcher = {
4949 observedBits: void | number | boolean,
5050 ): T,
5151 useState< S > (initialState: (() => S ) | S ) : [ S , Dispatch < BasicStateAction < S >> ] ,
52- useReducer < S , A > (
52+ useReducer < S , I , A > (
5353 reducer : ( S , A ) = > S ,
54- initialState : S ,
55- initialAction : A | void | null ,
54+ initialArg : I ,
55+ init ? : ( I ) => S ,
5656 ) : [ S , Dispatch < A > ] ,
5757 useContext < T > (
5858 context : ReactContext < T > ,
@@ -591,16 +591,17 @@ function updateContext<T>(
591591 return readContext(context, observedBits);
592592}
593593
594- function mountReducer < S , A > (
594+ function mountReducer < S , I , A > (
595595 reducer: (S, A) => S ,
596- initialState : void | S ,
597- initialAction : void | null | A ,
596+ initialArg : I ,
597+ init ?: I => S ,
598598) : [ S , Dispatch < A > ] {
599599 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 ) ;
604605 }
605606 hook.memoizedState = hook.baseState = initialState;
606607 const queue = (hook.queue = {
@@ -618,10 +619,10 @@ function mountReducer<S, A>(
618619 return [hook.memoizedState, dispatch];
619620}
620621
621- function updateReducer < S , A > (
622+ function updateReducer < S , I , A > (
622623 reducer: (S, A) => S ,
623- initialState : void | S ,
624- initialAction : void | null | A ,
624+ initialArg : I ,
625+ init ?: I => S ,
625626) : [ S , Dispatch < A > ] {
626627 const hook = updateWorkInProgressHook ( ) ;
627628 const queue = hook . queue ;
@@ -755,7 +756,6 @@ function mountState<S>(
755756 initialState: (() => S ) | S ,
756757) : [ S , Dispatch < BasicStateAction < S > > ] {
757758 const hook = mountWorkInProgressHook ( ) ;
758- // TODO: Lazy init API will change before release.
759759 if ( typeof initialState === 'function' ) {
760760 initialState = initialState ( ) ;
761761 }
@@ -1282,16 +1282,16 @@ if (__DEV__) {
12821282 ReactCurrentDispatcher . current = prevDispatcher ;
12831283 }
12841284 } ,
1285- useReducer < S , A > (
1285+ useReducer < S , I , A > (
12861286 reducer: (S, A) => S ,
1287- initialState : S ,
1288- initialAction : A | void | null ,
1287+ initialArg : I ,
1288+ init ?: I => S ,
12891289 ) : [ S , Dispatch < A > ] {
12901290 currentHookNameInDev = 'useReducer' ;
12911291 const prevDispatcher = ReactCurrentDispatcher . current ;
12921292 ReactCurrentDispatcher . current = InvalidNestedHooksDispatcherOnMountInDEV ;
12931293 try {
1294- return mountReducer ( reducer , initialState , initialAction ) ;
1294+ return mountReducer ( reducer , initialArg , init ) ;
12951295 } finally {
12961296 ReactCurrentDispatcher . current = prevDispatcher ;
12971297 }
@@ -1366,16 +1366,16 @@ if (__DEV__) {
13661366 ReactCurrentDispatcher . current = prevDispatcher ;
13671367 }
13681368 } ,
1369- useReducer < S , A > (
1369+ useReducer < S , I , A > (
13701370 reducer: (S, A) => S ,
1371- initialState : S ,
1372- initialAction : A | void | null ,
1371+ initialArg : I ,
1372+ init ?: I => S ,
13731373 ) : [ S , Dispatch < A > ] {
13741374 currentHookNameInDev = 'useReducer' ;
13751375 const prevDispatcher = ReactCurrentDispatcher . current ;
13761376 ReactCurrentDispatcher . current = InvalidNestedHooksDispatcherOnUpdateInDEV ;
13771377 try {
1378- return updateReducer ( reducer , initialState , initialAction ) ;
1378+ return updateReducer ( reducer , initialArg , init ) ;
13791379 } finally {
13801380 ReactCurrentDispatcher . current = prevDispatcher ;
13811381 }
@@ -1457,17 +1457,17 @@ if (__DEV__) {
14571457 ReactCurrentDispatcher . current = prevDispatcher ;
14581458 }
14591459 } ,
1460- useReducer < S , A > (
1460+ useReducer < S , I , A > (
14611461 reducer: (S, A) => S ,
1462- initialState : S ,
1463- initialAction : A | void | null ,
1462+ initialArg : I ,
1463+ init ?: I => S ,
14641464 ) : [ S , Dispatch < A > ] {
14651465 currentHookNameInDev = 'useReducer' ;
14661466 warnInvalidHookAccess ( ) ;
14671467 const prevDispatcher = ReactCurrentDispatcher . current ;
14681468 ReactCurrentDispatcher . current = InvalidNestedHooksDispatcherOnMountInDEV ;
14691469 try {
1470- return mountReducer ( reducer , initialState , initialAction ) ;
1470+ return mountReducer ( reducer , initialArg , init ) ;
14711471 } finally {
14721472 ReactCurrentDispatcher . current = prevDispatcher ;
14731473 }
@@ -1552,17 +1552,17 @@ if (__DEV__) {
15521552 ReactCurrentDispatcher . current = prevDispatcher ;
15531553 }
15541554 } ,
1555- useReducer < S , A > (
1555+ useReducer < S , I , A > (
15561556 reducer: (S, A) => S ,
1557- initialState : S ,
1558- initialAction : A | void | null ,
1557+ initialArg : I ,
1558+ init ?: I => S ,
15591559 ) : [ S , Dispatch < A > ] {
15601560 currentHookNameInDev = 'useReducer' ;
15611561 warnInvalidHookAccess ( ) ;
15621562 const prevDispatcher = ReactCurrentDispatcher . current ;
15631563 ReactCurrentDispatcher . current = InvalidNestedHooksDispatcherOnUpdateInDEV ;
15641564 try {
1565- return updateReducer ( reducer , initialState , initialAction ) ;
1565+ return updateReducer ( reducer , initialArg , init ) ;
15661566 } finally {
15671567 ReactCurrentDispatcher . current = prevDispatcher ;
15681568 }
0 commit comments