Skip to content

Instantly share code, notes, and snippets.

@jtmthf
Created June 3, 2021 20:09
Show Gist options
  • Select an option

  • Save jtmthf/ddc16fd365b5a6c3b748ff5103b4d1af to your computer and use it in GitHub Desktop.

Select an option

Save jtmthf/ddc16fd365b5a6c3b748ff5103b4d1af to your computer and use it in GitHub Desktop.
import { useMemo } from "react";
import { Interpreter } from "xstate";
import { useService } from "@xstate/react";
export function useAllowedEvents(service: Interpreter<any>) {
const [state] = useService(service);
const allowedEvents = useMemo(
() =>
state.nextEvents.filter(
(event) => service.machine.transition(state, event).changed
),
[service, state]
);
return allowedEvents;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment