Skip to content

Instantly share code, notes, and snippets.

@joshburgess
Created March 10, 2018 16:56
Show Gist options
  • Save joshburgess/925716725060c8127cb7ff8315944d3d to your computer and use it in GitHub Desktop.
Save joshburgess/925716725060c8127cb7ff8315944d3d to your computer and use it in GitHub Desktop.
import React from 'react'
import hoistNonReactStatics from 'hoist-non-react-statics'
export default function withFlexibleRender (OriginalComponent) {
class ComponentWithFlexibleRender extends OriginalComponent {
render () {
const { children, render } = this.props
const args = {
props: this.props,
state: this.state,
context: this.context,
}
return children
? (typeof children === 'function'
? children(args)
: children
)
: (render
? render(args)
: null
)
}
}
return hoistNonReactStatics(ComponentWithFlexibleRender, OriginalComponent)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment