Last active
January 5, 2017 18:52
-
-
Save tkh44/1f67da229a2f00e1ac25a4ea965f724d to your computer and use it in GitHub Desktop.
Basic example of using recompose's branch function
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
| // https://jsfiddle.net/tkh44/ub8s4deb/5/ | |
| const h = React.createElement | |
| const { branch, renderComponent } = Recompose | |
| const Box = (props) => { | |
| return h('pre', { style: { color: props.color, background: 'white', padding: 4 } }, | |
| `props: ${JSON.stringify(props, null, 2)}` | |
| ) | |
| } | |
| const wrapGreen = (Component) => (props) => { | |
| return h('div', { style: { color: 'white', background: 'green', padding: 8 } }, | |
| h(Component, props) | |
| ) | |
| } | |
| const wrapRed = (Component) => (props) => { | |
| return h('div', { style: { color: 'white', background: 'red', padding: 8 } }, | |
| h(Component, props) | |
| ) | |
| } | |
| const WrapBox = branch( | |
| ({ color }) => color === 'green', | |
| wrapGreen, | |
| wrapRed | |
| )(Box) | |
| ReactDOM.render( | |
| h(WrapBox, { color: 'green' }, 'This should render green'), | |
| document.getElementById('demo1') | |
| ) | |
| ReactDOM.render( | |
| h(WrapBox, { color: 'red' }, 'This should render red'), | |
| document.getElementById('demo2') | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment