Recompose is a React utility belt for function components and higher-order components. Think of it like lodash for React.
Full API documentation - Learn about each helper
Recompose Base Fiddle - Easy way to dive in
npm install recompose --save
📺 Watch Andrew's talk on Recompose at React Europe.
recompose-relay — Recompose helpers for Relay
Helpers like withState()
and withReducer()
provide a nicer way to express state updates:
const enhance = withState('counter', 'setCounter', 0)
const Counter = enhance(({ counter, setCounter }) =>
<div>
Count: {counter}
<button onClick={() => setCounter(n => n + 1)}>Increment</button>
<button onClick={() => setCounter(n => n - 1)}>Decrement</button>
</div>
)
Or with a Redux-style reducer:
const counterReducer = (count, action) => {
switch (action.type) {
case INCREMENT:
return count + 1
case DECREMENT:
return count - 1
default:
return count
}
}
const enhance = withReducer('counter', 'dispatch', counterReducer, 0)
const Counter = enhance(({ counter, dispatch }) =>
<div>
Count: {counter}
<button onClick={() => dispatch({ type: INCREMENT })}>Increment</button>
<button onClick={() => dispatch({ type: DECREMENT })}>Decrement</button>
</div>
)