@wordpress/data
Version:
Data module for WordPress.
8 lines (7 loc) • 2.75 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/components/use-dispatch/use-dispatch.ts"],
"sourcesContent": ["/**\n * Internal dependencies\n */\nimport useRegistry from '../registry-provider/use-registry';\nimport type {\n\tStoreDescriptor,\n\tAnyConfig,\n\tUseDispatchReturn,\n} from '../../types';\n\n/**\n * A custom react hook returning the current registry dispatch actions creators.\n *\n * Note: The component using this hook must be within the context of a\n * RegistryProvider.\n *\n * @param storeNameOrDescriptor Optionally provide the name of the\n * store or its descriptor from which to\n * retrieve action creators. If not\n * provided, the registry.dispatch\n * function is returned instead.\n *\n * @example\n * This illustrates a pattern where you may need to retrieve dynamic data from\n * the server via the `useSelect` hook to use in combination with the dispatch\n * action.\n *\n * ```jsx\n * import { useCallback } from 'react';\n * import { useDispatch, useSelect } from '@wordpress/data';\n * import { store as myCustomStore } from 'my-custom-store';\n *\n * function Button( { onClick, children } ) {\n * return <button type=\"button\" onClick={ onClick }>{ children }</button>\n * }\n *\n * const SaleButton = ( { children } ) => {\n * const { stockNumber } = useSelect(\n * ( select ) => select( myCustomStore ).getStockNumber(),\n * []\n * );\n * const { startSale } = useDispatch( myCustomStore );\n * const onClick = useCallback( () => {\n * const discountPercent = stockNumber > 50 ? 10: 20;\n * startSale( discountPercent );\n * }, [ stockNumber ] );\n * return <Button onClick={ onClick }>{ children }</Button>\n * }\n *\n * // Rendered somewhere in the application:\n * //\n * // <SaleButton>Start Sale!</SaleButton>\n * ```\n *\n * @return The dispatch function or action creators for the store.\n */\nconst useDispatch = <\n\tStoreNameOrDescriptor extends\n\t\t| undefined\n\t\t| string\n\t\t| StoreDescriptor< AnyConfig > = undefined,\n>(\n\tstoreNameOrDescriptor?: StoreNameOrDescriptor\n): UseDispatchReturn< StoreNameOrDescriptor > => {\n\tconst { dispatch } = useRegistry();\n\treturn (\n\t\tstoreNameOrDescriptor === void 0\n\t\t\t? dispatch\n\t\t\t: dispatch( storeNameOrDescriptor as StoreDescriptor< AnyConfig > )\n\t) as UseDispatchReturn< StoreNameOrDescriptor >;\n};\n\nexport default useDispatch;\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,0BAAwB;AAqDxB,IAAM,cAAc,CAMnB,0BACgD;AAChD,QAAM,EAAE,SAAS,QAAI,oBAAAA,SAAY;AACjC,SACC,0BAA0B,SACvB,WACA,SAAU,qBAAsD;AAErE;AAEA,IAAO,uBAAQ;",
"names": ["useRegistry"]
}