-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
66 lines (63 loc) · 1.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* WordPress dependencies
*/
import { createHigherOrderComponent, pure } from '@wordpress/compose';
/**
* Internal dependencies
*/
import useSelect from '../use-select';
/**
* 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
* function PriceDisplay( { price, currency } ) {
* return new Intl.NumberFormat( 'en-US', {
* style: 'currency',
* currency,
* } ).format( price );
* }
*
* const { withSelect } = wp.data;
*
* const HammerPriceDisplay = withSelect( ( select, ownProps ) => {
* const { getPrice } = select( 'my-shop' );
* 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 ) => createHigherOrderComponent(
( WrappedComponent ) => pure(
( ownProps ) => {
const mapSelect =
( select, registry ) => mapSelectToProps(
select,
ownProps,
registry
);
const mergeProps = useSelect( mapSelect );
return <WrappedComponent { ...ownProps } { ...mergeProps } />;
}
),
'withSelect'
);
export default withSelect;