Last active
September 7, 2017 20:41
-
-
Save darioghilardi/2f50dced5817b582f345d36732a22c9b to your computer and use it in GitHub Desktop.
Recompose: branch
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 React from 'react'; | |
import { render } from 'react-dom'; | |
import { branch, renderComponent, renderNothing } from 'recompose'; | |
// Define a stateless React component | |
const HelloWorld = () => <p>Hello World!</p>; | |
// Defines an higher order component using branch: | |
// - The first parameter is a function that | |
// returns true or false. In this case | |
// we check the value of the enabled prop. | |
// | |
// - The second parameter is the left condition, | |
// called when the condition is true. In this | |
// case we render the component HelloWorld. | |
// | |
// - The third optional parameter is the right | |
// condition, what happens when the condition | |
// is false. | |
const hoc = branch( | |
props => props.enabled === true, | |
renderComponent(HelloWorld), | |
renderNothing, | |
); | |
// To demonstrate how the defined higher order | |
// component works let's try to pass different | |
// values for the enabled prop. | |
const App = () => | |
<div> | |
<div> | |
<strong>With the enabled prop set to true:</strong> | |
{hoc()({ enabled: true })} | |
</div> | |
<div> | |
<strong>With the enabled prop set to false:</strong> | |
{hoc()({ enabled: false })} | |
</div> | |
</div>; | |
render(<App />, document.getElementById('root')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment