Skip to content

Instantly share code, notes, and snippets.

@gilesbutler
Created May 28, 2020 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilesbutler/44b5a79257f9f27a761c6fb2a4bf6f4c to your computer and use it in GitHub Desktop.
Save gilesbutler/44b5a79257f9f27a761c6fb2a4bf6f4c to your computer and use it in GitHub Desktop.
Simple Alert machine for xState
import { Machine, assign } from "xstate";
const initialState = {
alert: {},
};
export default Machine(
{
id: "alert",
initial: "idle",
context: {
...initialState,
},
states: {
idle: {
entry: "resetState",
on: {
ALERT: "active",
},
},
active: {
entry: "assignAlert",
on: {
CLEAR: "idle",
},
},
},
},
{
actions: {
assignAlert: assign({
alert: (context, event) => event?.alert,
}),
resetState: assign({
...initialState,
}),
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment