Skip to content

Instantly share code, notes, and snippets.

@benmvp
Last active August 29, 2017 06:20
Show Gist options
  • Save benmvp/69799153880d42d2ef2bb9f1ff9012b8 to your computer and use it in GitHub Desktop.
Save benmvp/69799153880d42d2ef2bb9f1ff9012b8 to your computer and use it in GitHub Desktop.
Need Dynamic HOC Flow help
const myHOC = <Props: {}>(
Component: React.ComponentType<{}>
): React.ComponentType<Props> (
(props: Props) = {
let handlers = genDynamicAdditionalProps(props.eventName)
// The keys in `additionalProps` are dependent upon `props.eventName` value
// The values in `additionalProps` are all functions
let propsToPassThru = {...props}
delete propsToPassThru[props.eventName]
// Also removing `props.eventName` from `props`!
return (
<Component
{...propsToPassThru}
{...handlers}
/>
)
}
)
@benmvp
Copy link
Author

benmvp commented Aug 29, 2017

Sorry, I missed your post! I would love to figure out a way to make it work if you're up for it. It'll certainly help me learn more Flow along the way.

So what I put in the gist is stripped down version of the full code, which is part of benmvp/react-composite-events#9. Essentially there's a function that takes a configuration of options that ends up returning the HOC above. So props.eventName in the example above ends up being props[eventPropName], making it even more dynamic.

In case you're curious what this is all about, here are the docs for the utility function: https://github.com/benmvp/react-composite-events/blob/master/src/compose.md

Thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment