/** * WordPress dependencies */ import { useContext } from '@wordpress/element'; /** * Internal dependencies */ import { Context } from './context'; import type { DataRegistry } from '../../types'; /** * A custom react hook exposing the registry context for use. * * This exposes the `registry` value provided via the * Registry Provider to a component implementing * this hook. * * It acts similarly to the `useContext` react hook. * * Note: Generally speaking, `useRegistry` is a low level hook that in most cases * won't be needed for implementation. Most interactions with the `@wordpress/data` * API can be performed via the `useSelect` hook, or the `withSelect` and * `withDispatch` higher order components. * * @example * ```js * import { * RegistryProvider, * createRegistry, * useRegistry, * } from '@wordpress/data'; * * const registry = createRegistry( {} ); * * const SomeChildUsingRegistry = ( props ) => { * const registry = useRegistry(); * // ...logic implementing the registry in other react hooks. * }; * * * const ParentProvidingRegistry = ( props ) => { * return * * * }; * ``` * * @return A custom react hook exposing the registry context value. */ export default function useRegistry(): DataRegistry { return useContext( Context ); }