React hooks combine
Hooks powered, recompose like utility belt for ladies and gentlemen.
React Hooks Combine (RHC) is a simple utility belt to help you split up logic for your components between container (smart) part and dummy component (view) part.
It helps you to create only one HOC effortlessly with custom hook which combines all listed HOOKS (all in one).
It has API Design similar to Recompose.
Documentation
The documentation is divided into several sections:
You can improve it by sending pull requests to this repository.
Let's Get Started
Prerequisites:
Install (choose preferable way)
yarn add react-hooks-combine
npm install react-hooks-combine
Then...
import React from 'react'
export const CounterComponent = ({ count, onPlus, onMinus }) => (
<div>
<strong>Active: {count}</strong>
<button type="button" onClick={onPlus}>+</button>
<button type="button" onClick={onMinus}>-</button>
</div>
)
import { combine, withReducer, withCallbacks } from 'react-hooks-combine'
import { CounterComponent } from './component'
const INC = 'INC'
const DEC = 'DEC'
const reducer = (count, action) => {
switch(action.type) {
case INC: return count + 1
case DEC: return count - 1
default: return count
}
}
export default combine(
withReducer({
reducer,
stateName: counterState,
initialState: 0,
}),
withCallbacks({
onPlus: ({ counterState, dispatch }, _props) => () => {
dispatchFn({ type: INC })
},
onMinus: ({ counterState, dispatch }, _props) => () => {
dispatchFn({ type: DEC })
}
}, ['counterState']),
)
OR
import { combine, withState, withCallbacks } from 'react-hooks-combine'
import { CounterComponent } from './component'
export default combine(
withState('count', 'setCount', 0),
withCallbacks({
onPlus: ({ count, setCount }, _props) => () => {
setCount(count + 1)
},
onMinus: ({ setCount }, _props) => () => {
setCount(count => count - 1)
}
}, ['count']),
)
OR WHICHEVER YOU LIKE...