@@ -11,13 +11,16 @@ const useReducerWithMiddleware = (
1111) : [ State , ( action : Action ) => void ] => {
1212 const [ state , dispatch ] = React . useReducer ( reducer , controlledState ) ;
1313
14+ const sRef = React . useRef < State | null > ( null ) ;
1415 const aRef = React . useRef < Action | null > ( null ) ;
1516
1617 const dispatchWithMiddleware = ( action : Action ) => {
1718 middlewareFns . forEach ( ( middlewareFn ) =>
1819 middlewareFn ( action , state , context ? context . current : undefined ) ,
1920 ) ;
2021
22+ const nextState = reducer ( state , action ) ;
23+ sRef . current = nextState ;
2124 aRef . current = action ;
2225
2326 dispatch ( action ) ;
@@ -27,10 +30,11 @@ const useReducerWithMiddleware = (
2730 if ( ! aRef . current ) return ;
2831
2932 afterwareFns . forEach ( ( afterwareFn ) =>
30- afterwareFn ( aRef . current ! , state , context ? context . current : undefined ) ,
33+ afterwareFn ( aRef . current ! , sRef . current ! , context ? context . current : undefined ) ,
3134 ) ;
3235
3336 aRef . current = null ;
37+ sRef . current = null ;
3438 } , [ context , afterwareFns , state ] ) ;
3539
3640 return [ state , dispatchWithMiddleware ] ;
0 commit comments