Skip to content

Instantly share code, notes, and snippets.

@James-E-Adams
Last active October 23, 2018 05:13
Show Gist options
  • Save James-E-Adams/aa3c2fa327f0de9fd9d8b90f9c8cb72c to your computer and use it in GitHub Desktop.
Save James-E-Adams/aa3c2fa327f0de9fd9d8b90f9c8cb72c to your computer and use it in GitHub Desktop.
Functional React Article Snippet #3
// Or better:
// same imports as before plus...
import compose from 'recompose/compose'
const Farm = compose(
withState("tools", "setTools", ["hammer", "scythe", "sickle"]),
lifecycle({ didMount }),
branch(shouldNotRender, renderNothing),
onlyUpdateForKeys(["tools"])
)(BaseFarm)
// Alternatively.
const enhance = compose(
withState("tools", "setTools", ["hammer", "scythe", "sickle"]),
lifecycle({ didMount }),
branch(shouldNotRender, renderNothing),
onlyUpdateForKeys(["tools"])
)
// enhance is a HOC, composed of a few other HOCs.
// It takes a component (BaseFarm) and returns Farm.
const Farm = enhance(({ someProp, anotherProp, tools }) => (
<div>
Here are all the tools in the farm:
<ul>
{tools.map((tool, index) => (
<li key={index}> {tool} </li>
))}
</ul>
{someProp} - {anotherProp}
</div>
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment