@@ -16,7 +16,7 @@ interface IState {
1616}
1717
1818describe ( 'redux-react-hook' , ( ) => {
19- let subscriberCallback : ( ( ) => void ) | null ;
19+ let subscriberCallbacks : Array < ( ) => void > = [ ] ;
2020 let state : IState ;
2121 let cancelSubscription : ( ) => void ;
2222 let store : Store < IState , IAction > ;
@@ -26,19 +26,23 @@ describe('redux-react-hook', () => {
2626 dispatch : jest . fn ( action => action ) ,
2727 getState : ( ) => state ,
2828 subscribe : jest . fn ( ( l : ( ) => void ) => {
29- subscriberCallback = l ;
29+ subscriberCallbacks . push ( l ) ;
3030 return cancelSubscription ;
3131 } ) ,
3232 // tslint:disable-next-line:no-empty
3333 replaceReducer ( ) { } ,
3434 } ) ;
3535
36- function updateStore ( newState : IState ) {
36+ function updateStoreWithoutAct ( newState : IState ) {
3737 state = newState ;
38+ for ( const sub of subscriberCallbacks ) {
39+ sub ( ) ;
40+ }
41+ }
42+
43+ function updateStore ( newState : IState ) {
3844 act ( ( ) => {
39- if ( subscriberCallback ) {
40- subscriberCallback ( ) ;
41- }
45+ updateStoreWithoutAct ( newState ) ;
4246 } ) ;
4347 }
4448
@@ -53,7 +57,7 @@ describe('redux-react-hook', () => {
5357
5458 afterEach ( ( ) => {
5559 document . body . removeChild ( reactRoot ) ;
56- subscriberCallback = null ;
60+ subscriberCallbacks = [ ] ;
5761 } ) ;
5862
5963 function render ( element : React . ReactElement < any > ) {
@@ -265,6 +269,25 @@ describe('redux-react-hook', () => {
265269 expect ( renderCount ) . toBe ( 2 ) ;
266270 } ) ;
267271
272+ it ( 'renders once if have multiple useMappedState' , ( ) => {
273+ let renderCount = 0 ;
274+ const Component = ( { prop} : { prop : any } ) => {
275+ const mapState1 = React . useCallback ( ( s : IState ) => s . bar , [ prop ] ) ;
276+ const mapState2 = React . useCallback ( ( s : IState ) => s . foo , [ prop ] ) ;
277+ useMappedState ( mapState1 ) ;
278+ useMappedState ( mapState2 ) ;
279+ renderCount ++ ;
280+ return null ;
281+ } ;
282+
283+ render ( < Component prop = { 1 } /> ) ;
284+
285+ updateStoreWithoutAct ( { bar : 11 , foo : '11' } ) ;
286+ updateStoreWithoutAct ( { bar : 12 , foo : '12' } ) ;
287+ act ( ( ) => { } ) ;
288+ expect ( renderCount ) . toBe ( 3 ) ;
289+ } ) ;
290+
268291 it ( 'throws if provider is missing' , ( ) => {
269292 const Component = ( ) => {
270293 const mapState = React . useCallback ( ( s : IState ) => s , [ ] ) ;
0 commit comments