yieldmachine icon indicating copy to clipboard operation
yieldmachine copied to clipboard

Support being reducer function passed to React’s `useReducer` accepting an event as action

Open RoyalIcing opened this issue 3 years ago • 0 comments

function reducer(previousState: State, event: React.FormEvent<HTMLFormElement> | React.MouseEvent<HTMLButtonElement>): State {
  return previousState.machine.next(event);
}

eventsReducer(machine)

It wouldn’t support side effects and listening/emitting to events (e.g. with listenTo()).

import { eventsReducer } from 'yieldmachine';

function useMachine(machine) {
  const [state, dispatch] = useReducer(eventsReducer(machine));
  return [state, dispatch];
}

RoyalIcing avatar Nov 11 '21 00:11 RoyalIcing