-
-
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
This file contains 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 { 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