Last active
February 21, 2017 17:31
-
-
Save webpapaya/8aefa6588dae67c3c40210462466d9e5 to your computer and use it in GitHub Desktop.
Prepare react props before pushing them to a child
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
// Executes the given fn with the props and applies the result to the component | |
const prepareProps = (fn) => (Component) => (props) => { | |
return <Component { ...fn(props) } /> | |
}; | |
const _Child = ({ test }) => { | |
return ( | |
<div> | |
{ test /* rendered as "MY PROPERTY" */ } | |
</div> | |
); | |
}; | |
// Modify the props from the parent | |
// This fn can easily be unit tested | |
const prepareChildsProps = ({ test }) => ({ test: test.toUpperCase() }); | |
// "Marry" the prepare fn with the _Child component | |
const Child = prepareProps(prepareChildsProps)(_Child); | |
// Simply render something | |
const Parent = () => { | |
return <Child test="my property" /> | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice idea, maybe combining prepareProps with reducers would make the use more generic.