-
Hi, we have a machine that currently all transitions might happen. I.e: {
id: 'letters',
initial: 'a',
context: {},
on: {
MANUAL_TRANSITION: ???
},
states: {
a: {},
b: {},
c: {},
d: {},
e: {},
f: {}
}
} As you can see the events are in the root level of the machine. Is this behaviour can be achieved? Other alternative is to create an event for every possible state: on: {
a: 'a',
b: 'b',
c: 'c'
...
} but that doesn't sounds right. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
This is not possible this way - u cant have dynamic targets. All transitions have to be statically defined so then can be visualized etc. Not knowing what problem you are trying to solve makes it harder to suggest viable alternatives. |
Beta Was this translation helpful? Give feedback.
-
This seems like a contrived example, but generally speaking, when you want a "dynamic state" based on the event, that's a sign that that state isn't really a finite state, but more of an extended state (belonging in However, if you really want to do this, and you do have a finite number of states, build it out: const states = ['a', 'b', 'c', 'd', /* ... */];
const createSomeMachine = () => {
return createMachine({
// ...
states: states.reduce((acc, key) => {
acc[key] = {};
return acc;
}, {}),
on: {
MANUAL_TRANSITION: states.map(state => {
return { target: state, cond: (_, event) => event.target === state }
})
}
});
} |
Beta Was this translation helpful? Give feedback.
-
Faced same necessity. What I came up with is to use |
Beta Was this translation helpful? Give feedback.
This is not possible this way - u cant have dynamic targets. All transitions have to be statically defined so then can be visualized etc. Not knowing what problem you are trying to solve makes it harder to suggest viable alternatives.