UNPKG

@wordpress/data

Version:
77 lines (65 loc) 2.37 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _element = require("@wordpress/element"); var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _compose = require("@wordpress/compose"); var _useSelect = _interopRequireDefault(require("../use-select")); /** * WordPress dependencies */ /** * Internal dependencies */ /** @typedef {import('@wordpress/element').WPComponent} WPComponent */ /** * Higher-order component used to inject state-derived props using registered * selectors. * * @param {Function} mapSelectToProps Function called on every state change, * expected to return object of props to * merge with the component's own props. * * @example * ```js * import { withSelect } from '@wordpress/data'; * import { store as myCustomStore } from 'my-custom-store'; * * function PriceDisplay( { price, currency } ) { * return new Intl.NumberFormat( 'en-US', { * style: 'currency', * currency, * } ).format( price ); * } * * const HammerPriceDisplay = withSelect( ( select, ownProps ) => { * const { getPrice } = select( myCustomStore ); * const { currency } = ownProps; * * return { * price: getPrice( 'hammer', currency ), * }; * } )( PriceDisplay ); * * // Rendered in the application: * // * // <HammerPriceDisplay currency="USD" /> * ``` * In the above example, when `HammerPriceDisplay` is rendered into an * application, it will pass the price into the underlying `PriceDisplay` * component and update automatically if the price of a hammer ever changes in * the store. * * @return {WPComponent} Enhanced component with merged state data props. */ const withSelect = mapSelectToProps => (0, _compose.createHigherOrderComponent)(WrappedComponent => (0, _compose.pure)(ownProps => { const mapSelect = (select, registry) => mapSelectToProps(select, ownProps, registry); const mergeProps = (0, _useSelect.default)(mapSelect); return (0, _element.createElement)(WrappedComponent, (0, _extends2.default)({}, ownProps, mergeProps)); }), 'withSelect'); var _default = withSelect; exports.default = _default; //# sourceMappingURL=index.js.map