Last active
October 22, 2018 20:21
-
-
Save James-E-Adams/35cb793f072443c00ed89e0a600566e1 to your computer and use it in GitHub Desktop.
Functional React Article Snippet #2
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
import branch from 'recompose/branch' | |
import withState from 'recompose/withState' | |
import renderNothing from 'recompose/renderNothing' | |
import onlyUpdateForKeys from 'recompose/onlyUpdateForKeys' | |
import lifecycle from 'out-of-scope-for-this-article-but-serves-same-purpose-as-recompose-lifecycle' | |
// And now the exact same thing, written functionally. | |
//---------------------------------------------------------------// | |
// Only the 'view' logic. | |
const BaseFarm = ({ 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> | |
) | |
// All the 'other' stuff. | |
const didMount = ({ setTools }) => | |
fetch("/tools") | |
.then(({ body: { tools } }) => setTools(tools)) | |
.catch(err => console.log("Oh no, your tools got lost. Here's why: ", err)) | |
const shouldNotRender = ({ tools }) => !(tools && tools.length) | |
const Farm = withState("tools", "setTools", ["hammer", "scythe", "sickle"])( | |
// N.B: lifecycle isn't the one from recompose | |
lifecycle({ didMount })( | |
branch(shouldNotRender, renderNothing)( | |
onlyUpdateForKeys(["tools"])(BaseFarm) | |
) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment