matthewp / robot

🤖 A functional, immutable Finite State Machine library
https://thisrobot.life
BSD 2-Clause "Simplified" License
1.92k stars 88 forks source link

The machine's current state is not updated when send is triggered #192

Open gaofengye opened 2 years ago

gaofengye commented 2 years ago

Hello Creator of react-robot,

I've been just starting to use react-robot in React 18, and I noticed that the "current" property provided from the hook "useMachine" doesn't update when I call "send('next')". The code used is the one from the documentation:

import { useMachine } from 'react-robot'; import React from 'react'; import { createMachine, state, transition } from 'robot3';

const machine = createMachine({ one: state( transition('next', 'two') ), two: state() });

function App() { const [current, send] = useMachine(machine);

return ( <button type="button" onClick={() => send('next')}>State: ${current.name}<\/button> ); }

Any idea on how to solve this? Thank you

gaofengye commented 2 years ago

Found a way to solve it, by replacing mounted = true by const [isMounted, setIsMounted] = useState(true); of createUseMachine function in the library robot-hooks