Skip to content

Instantly share code, notes, and snippets.

@ernestofreyreg
Created October 27, 2018 15:59
Show Gist options
  • Save ernestofreyreg/cf9df19f9195b05aee7da8e97918ca0d to your computer and use it in GitHub Desktop.
Save ernestofreyreg/cf9df19f9195b05aee7da8e97918ca0d to your computer and use it in GitHub Desktop.
import React from 'react'
function useStateManager({ initialState, reducer, createActions }) {
const [state, dispatch] = React.useReducer(reducer, initialState)
const actions = createActions({
getState: () => state,
dispatch: (type, payload) =>
Promise.resolve(dispatch({ type, ...(payload || {}) }))
})
return [state, actions]
}
export default useStateManager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment