Update `useQueryStateByKey` to return callback which wraps the dispatcher (https://github.com/woocommerce/woocommerce-blocks/pull/1115)

* Update useQueryStateByKey to return callback wrapping dispatch

* Fix test
This commit is contained in:
Mike Jolley 2019-11-01 12:33:42 +00:00 committed by GitHub
parent 332c4ccce2
commit 85faacd120
2 changed files with 15 additions and 5 deletions

View File

@ -193,9 +193,11 @@ describe( 'Testing Query State Hooks', () => {
act( () => {
setQueryState( { foo: 'bar' } );
} );
expect( action ).toHaveBeenCalledWith( {
foo: 'bar',
} );
expect( action ).toHaveBeenCalledWith(
'test-context',
'someValue',
{ foo: 'bar' }
);
}
);
} );

View File

@ -3,7 +3,7 @@
*/
import { QUERY_STATE_STORE_KEY as storeKey } from '@woocommerce/block-data';
import { useSelect, useDispatch } from '@wordpress/data';
import { useRef, useEffect } from '@wordpress/element';
import { useRef, useEffect, useCallback } from '@wordpress/element';
import { useShallowEqual } from './use-shallow-equal';
/**
@ -52,8 +52,16 @@ export const useQueryStateByKey = ( context, queryKey ) => {
},
[ context, queryKey ]
);
const { setQueryValue } = useDispatch( storeKey );
return [ queryValue, setQueryValue ];
const setQueryValueByKey = useCallback(
( value ) => {
setQueryValue( context, queryKey, value );
},
[ context, queryKey ]
);
return [ queryValue, setQueryValueByKey ];
};
/**