Created
September 13, 2018 04:06
-
-
Save thulioph/1940a1916b0ce896c0bd3fc78a2609d5 to your computer and use it in GitHub Desktop.
An example to demonstrate the withHandlers API of Recompose
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
import React from 'react'; | |
import { compose, withState, withHandlers } from 'recompose'; | |
const MyComponent = ({ counter, increment, decrement, reset }) => ( | |
<div> | |
<h2>{counter}</h2> | |
<button onClick={increment}>Add</button> | |
<button onClick={decrement}>Remove</button> | |
<button onClick={reset}>Reset</button> | |
</div> | |
); | |
const MyComponentModified = compose( | |
withState("counter", "setCounter", 0), | |
withHandlers({ | |
increment: ({ setCounter }) => () => setCounter(n => n + 1), | |
decrement: ({ setCounter }) => () => setCounter(n => n - 1), | |
reset: ({ setCounter }) => () => setCounter(0) | |
}), | |
)(MyComponent); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment