UNPKG

haunted

Version:
32 lines (31 loc) 1.17 kB
import { hook, Hook } from "./hook"; /** * Given a reducer function, initial state, and optional state initializer function, returns a tuple of state and dispatch function. * @function * @template S State * @template I Initial State * @template A Action * @param {Reducer<S, A>} reducer - reducer function to compute the next state given the previous state and the action * @param {I} initialState - the initial state of the reducer * @param {(init: I) => S} [init=undefined] - Optional initializer function, called on initialState if provided * @return {readonly [S, (action: A) => void]} */ const useReducer = hook(class extends Hook { reducer; currentState; constructor(id, state, _, initialState, init) { super(id, state); this.dispatch = this.dispatch.bind(this); this.currentState = init !== undefined ? init(initialState) : initialState; } update(reducer) { this.reducer = reducer; return [this.currentState, this.dispatch]; } dispatch(action) { this.currentState = this.reducer(this.currentState, action); this.state.update(); } }); export { useReducer };