Skip to content

Instantly share code, notes, and snippets.

@jasonrhodes
Last active May 17, 2018 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonrhodes/c2e6f772d0b2b5a2b3818bc4742576b9 to your computer and use it in GitHub Desktop.
Save jasonrhodes/c2e6f772d0b2b5a2b3818bc4742576b9 to your computer and use it in GitHub Desktop.
React HOCs and "Render Props" ... Note: If we're going to call it the "render prop" pattern, can we stop using props.children as the function?
// higher order component creator
const withFun = (Component) => (props) => <Component {...props} fun='Injected Secret Value' />
// using the higher order component (with this pattern, you have to create a component, THEN wrap it in the HOC creator function)
const _MyComponent = (props) => <div><h1>{props.fun}</h1></div>
const MyComponent = withFun(_MyComponent)
// "render prop" using children
const FunChildren = ({ children }) => children({ fun: 'Injected Secret Value' })
// using render prop as children ... why is this called "render prop" you might ask??
const MyComponent = (props) => (
<div>
<FunChildren>
{({ fun }) => <h1>{fun}</h1>}
</FunChildren>
</div>
)
// "render prop" ... as a prop called render
const FunRenderProp = ({ render }) => render({ fun: 'Injected Secret Value' })
// using render prop ... ah THIS makes sense to be called "render prop"
const MyComponent = (props) => (
<div>
<FunRenderProp render={({ fun }) => <h1>{fun}</h1>} />
</div>
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment