@nozbe/watermelondb
Version:
Build powerful React Native and React web apps that scale from hundreds to tens of thousands of records and remain fast
25 lines (22 loc) • 915 B
JavaScript
// @flow
import { type HOC, createFactory } from './helpers'
type GetNewProps = <T: { ... }>(T) => T
type Props$Merge<A, B> = { ...$Exact<A>, ...$Exact<B> }
type EnhancedProps<PropsInput, NewProps> = Props$Merge<PropsInput, $Call<GetNewProps, NewProps>>
export default function withHooks<PropsInput: { ... }, NewProps: { ... }>(
hookTransformer: (props: PropsInput) => NewProps,
): HOC<EnhancedProps<PropsInput, NewProps>, PropsInput> {
return (BaseComponent) => {
const factory = createFactory(BaseComponent)
const enhanced = function WithHooks(props: any): any {
const newProps = hookTransformer(props)
return factory({ ...props, ...newProps })
}
if (process.env.NODE_ENV !== 'production') {
const baseName = BaseComponent.displayName || BaseComponent.name || 'anon'
// $FlowFixMe
enhanced.displayName = `withHooks[${baseName}]`
}
return enhanced
}
}