Created
May 28, 2020 20:29
-
-
Save gilesbutler/44b5a79257f9f27a761c6fb2a4bf6f4c to your computer and use it in GitHub Desktop.
Simple Alert machine for xState
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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