-
Notifications
You must be signed in to change notification settings - Fork 52
/
selectors.js
35 lines (31 loc) · 926 Bytes
/
selectors.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
import React from 'react'
import hoistNonReactStatic from 'hoist-non-react-statics'
// ref: https://reactjs.org/docs/higher-order-components.html
function withSelector(selector) {
return WrappedComponent => {
class Enhance extends React.PureComponent {
render() {
const { data, ...passThrough } = this.props
const processedData = selector(this.props)
return (
<WrappedComponent
{...passThrough}
mutate={data.mutate}
refetch={data.refetch}
data={processedData}
/>
)
}
}
Enhance.displayName = `withSelector(${getDisplayName(WrappedComponent)})`
hoistNonReactStatic(Enhance, WrappedComponent)
return Enhance
}
}
function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || 'Component'
}
export default {
withSelector,
getDisplayName,
}