Expectation about activities! #1652
-
I tried to do "activities" to update the context with |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Can you share code? If you want some external entity (an "activity" or whatever you want to call it) to signal to the machine that it should update context on an interval, you can do it like this: const machine = createMachine({
// ...
invoke: {
src: () => sendBack => {
const i = setInterval(() => {
sendBack('UPDATE');
}, 1000);
return () => clearInterval(i);
}
},
on: {
UPDATE: { actions: assign({ count: ctx => ctx.count + 1 }) }
}
}); |
Beta Was this translation helpful? Give feedback.
-
here is my code, const machine = createMachine({
// ...
{
activities: {
UPDATE: () => {
const i = setInterval(() => {
assign({
value: (c) => c.value + 1
});
}, 1000);
return () => clearInterval(i);
},
}) React part const [state] =useMachine(machine)
const {value}= state.context
const [localstate, setLocalstate]= useState(value)
useEffect(()=>setLocalstate() {
const i = setInterval(()=>setLocalstate(value), 1000)
// cleanup...
}, [value])
// ... |
Beta Was this translation helpful? Give feedback.
Can you share code?
If you want some external entity (an "activity" or whatever you want to call it) to signal to the machine that it should update context on an interval, you can do it like this: