UNPKG

@wordpress/compose

Version:
32 lines (29 loc) 1.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useObservableValue; var _element = require("@wordpress/element"); /** * WordPress dependencies */ /** * Internal dependencies */ /** * React hook that lets you observe an entry in an `ObservableMap`. The hook returns the * current value corresponding to the key, or `undefined` when there is no value stored. * It also observes changes to the value and triggers an update of the calling component * in case the value changes. * * @template K The type of the keys in the map. * @template V The type of the values in the map. * @param map The `ObservableMap` to observe. * @param name The map key to observe. * @return The value corresponding to the map key requested. */ function useObservableValue(map, name) { const [subscribe, getValue] = (0, _element.useMemo)(() => [listener => map.subscribe(name, listener), () => map.get(name)], [map, name]); return (0, _element.useSyncExternalStore)(subscribe, getValue, getValue); } //# sourceMappingURL=index.js.map