-
Notifications
You must be signed in to change notification settings - Fork 1
/
withGAEvent.js
32 lines (26 loc) · 882 Bytes
/
withGAEvent.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
import React from 'react'
import GAEventContext from './GAEventContext'
// HOC to add `sendEvent` function respecting refs
export function withGAEvent (Component) {
// Wrapper component
function GAEvent (props) {
const { forwardedRef, ...restProps } = props
// Get `sendEvent` from context and pass it to the wrapped
// component, along with ref and the rest of props
return (
<GAEventContext.Consumer>
{(sendEvent) => (
<Component ref={forwardedRef} {...restProps} {...{ sendEvent }} />
)}
</GAEventContext.Consumer>
)
}
// Return wrapper respecting ref
const forwarded = React.forwardRef((props, ref) => {
return <GAEvent {...props} forwardedRef={ref} />
})
forwarded.propTypes = Component.propTypes
forwarded.defaultProps = Component.defaultProps
return forwarded
}
export default withGAEvent