Created
August 1, 2019 14:04
-
-
Save OliverJAsh/15a42ad27e09b20026681d35127a5739 to your computer and use it in GitHub Desktop.
TypeScript + React: convert render prop component to HOC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ComponentType, ReactElement } from 'react'; | |
import React = require('react'); | |
type RenderPropComponent = ComponentType<{ children: () => ReactElement }>; | |
const RenderProp: RenderPropComponent = ({ children }) => ( | |
<div>{children()}</div> | |
); | |
const renderPropToHoc = (RenderPropComponent: RenderPropComponent) => <P,>( | |
ComposedComponent: ComponentType<P>, | |
) => (props: P) => ( | |
<RenderPropComponent> | |
{() => <ComposedComponent {...props} />} | |
</RenderPropComponent> | |
); | |
const HOC = renderPropToHoc(RenderProp); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment