Skip to content

Instantly share code, notes, and snippets.

@carlosbensant
Created May 29, 2024 01:24
Show Gist options
  • Save carlosbensant/21adcd22373d16f1388f283a9b53a17f to your computer and use it in GitHub Desktop.
Save carlosbensant/21adcd22373d16f1388f283a9b53a17f to your computer and use it in GitHub Desktop.
xstate workaround - as inner state actions with previous state, not active/current state
import { setup, assign } from 'xstate';
const createDogMachine = (contextFromDB) => {
return setup({
actions: {
updateContext: assign({
state: (_, params) => params.state,
}),
}
}).createMachine({
id: 'dog',
initial: contextFromDB.state || 'asleep',
context: contextFromDB,
states: {
asleep: {
entry: [
{
type: 'updateContext',
params: {
state: 'asleep', // current state
}
}
],
on: {
'wakes up': 'awake',
}
},
awake: {
entry: [
{
type: 'updateContext',
params: {
state: 'awake', // current state
}
}
],
on: {
'falls asleep': 'asleep',
}
},
//...
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment