Skip to content

Instantly share code, notes, and snippets.

@tkh44
Last active January 5, 2017 18:52
Show Gist options
  • Select an option

  • Save tkh44/1f67da229a2f00e1ac25a4ea965f724d to your computer and use it in GitHub Desktop.

Select an option

Save tkh44/1f67da229a2f00e1ac25a4ea965f724d to your computer and use it in GitHub Desktop.
Basic example of using recompose's branch function
// 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