UNPKG

react-stateful-function

Version:
11 lines (9 loc) 565 B
import { useReducer } from 'react'; const mini_reducer = (state = {}, action) => ({ ...state, [action.type]: action.payload }); const useInitialState = (initialState = {}) => { const [state, dispatch] = useReducer(mini_reducer, initialState); const setState = (fState = {}, callback = () => null) => { for (const type in fState) { dispatch({ type, payload: fState[type] }); } callback(); }; const resetState = (callback = () => null) => { setState(initialState); callback() }; return { state, setState, resetState }; }; export { useInitialState };