UNPKG

@wordpress/data

Version:
109 lines (105 loc) 4.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _compose = require("@wordpress/compose"); var _useDispatch = require("../use-dispatch"); var _jsxRuntime = require("react/jsx-runtime"); /** * WordPress dependencies */ /** * Internal dependencies */ /** @typedef {import('react').ComponentType} ComponentType */ /** * Higher-order component used to add dispatch props using registered action * creators. * * @param {Function} mapDispatchToProps A function of returning an object of * prop names where value is a * dispatch-bound action creator, or a * function to be called with the * component's props and returning an * action creator. * * @example * ```jsx * function Button( { onClick, children } ) { * return <button type="button" onClick={ onClick }>{ children }</button>; * } * * import { withDispatch } from '@wordpress/data'; * import { store as myCustomStore } from 'my-custom-store'; * * const SaleButton = withDispatch( ( dispatch, ownProps ) => { * const { startSale } = dispatch( myCustomStore ); * const { discountPercent } = ownProps; * * return { * onClick() { * startSale( discountPercent ); * }, * }; * } )( Button ); * * // Rendered in the application: * // * // <SaleButton discountPercent="20">Start Sale!</SaleButton> * ``` * * @example * In the majority of cases, it will be sufficient to use only two first params * passed to `mapDispatchToProps` as illustrated in the previous example. * However, there might be some very advanced use cases where using the * `registry` object might be used as a tool to optimize the performance of * your component. Using `select` function from the registry might be useful * when you need to fetch some dynamic data from the store at the time when the * event is fired, but at the same time, you never use it to render your * component. In such scenario, you can avoid using the `withSelect` higher * order component to compute such prop, which might lead to unnecessary * re-renders of your component caused by its frequent value change. * Keep in mind, that `mapDispatchToProps` must return an object with functions * only. * * ```jsx * function Button( { onClick, children } ) { * return <button type="button" onClick={ onClick }>{ children }</button>; * } * * import { withDispatch } from '@wordpress/data'; * import { store as myCustomStore } from 'my-custom-store'; * * const SaleButton = withDispatch( ( dispatch, ownProps, { select } ) => { * // Stock number changes frequently. * const { getStockNumber } = select( myCustomStore ); * const { startSale } = dispatch( myCustomStore ); * return { * onClick() { * const discountPercent = getStockNumber() > 50 ? 10 : 20; * startSale( discountPercent ); * }, * }; * } )( Button ); * * // Rendered in the application: * // * // <SaleButton>Start Sale!</SaleButton> * ``` * * _Note:_ It is important that the `mapDispatchToProps` function always * returns an object with the same keys. For example, it should not contain * conditions under which a different value would be returned. * * @return {ComponentType} Enhanced component with merged dispatcher props. */const withDispatch = mapDispatchToProps => (0, _compose.createHigherOrderComponent)(WrappedComponent => ownProps => { const mapDispatch = (dispatch, registry) => mapDispatchToProps(dispatch, ownProps, registry); const dispatchProps = (0, _useDispatch.useDispatchWithMap)(mapDispatch, []); return /*#__PURE__*/(0, _jsxRuntime.jsx)(WrappedComponent, { ...ownProps, ...dispatchProps }); }, 'withDispatch'); var _default = exports.default = withDispatch; //# sourceMappingURL=index.js.map