Last active
May 17, 2018 16:00
-
-
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?
This file contains 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
// 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