Skip to content

Instantly share code, notes, and snippets.

@thulioph
Created September 13, 2018 04:06
Show Gist options
  • Save thulioph/1940a1916b0ce896c0bd3fc78a2609d5 to your computer and use it in GitHub Desktop.
Save thulioph/1940a1916b0ce896c0bd3fc78a2609d5 to your computer and use it in GitHub Desktop.
An example to demonstrate the withHandlers API of Recompose
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