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:
parent
332c4ccce2
commit
85faacd120
|
@ -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' }
|
||||
);
|
||||
}
|
||||
);
|
||||
} );
|
||||
|
|
|
@ -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 ];
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue