-
I write the state machine in the same file as a React component.
As a result, I need to manually refresh the browser each time. So Vite hot reload is useless to me. How can I make it work with Vite hot reload? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
We are aware of the issues with hot-reloading and plan to work on fixing them. Unfortunately, this is just broken at the moment - it's because we are not capable (yet) of rehydrating the whole state into a new, incoming, machine (the new machine, the fast-refreshed one, can be completely different from the previous one after all). |
Beta Was this translation helpful? Give feedback.
-
A workaround I've been using is extracting a view component and moving it to a separate file, e.g. this: // Foo.tsx
import { useMachine } from "@xstate/react";
const Foo: React.FC = () => {
const [state, send] = useMachine(fooMachine);
return (
<div>
{state.matches("ready") && <button onClick={() => send("EVENT")} />}
</div>
);
}; Becomes this: // Foo.tsx
import { useMachine } from "@xstate/react";
import { fooMachine } from "./foo.machine";
import { FooView } from "./FooView";
const Foo: React.FC = () => {
const [state, send, service] = useMachine(fooMachine);
return <FooView service={service} state={state} />;
};
// FooView.tsx
import { fooMachine } from "./foo.machine";
import { InterpreterFrom, StateFrom } from "xstate";
const FooView: React.FC<{
service: InterpreterFrom<typeof myMachine>;
state: StateFrom<typeof myMachine>;
}> = ({ service, state }) => {
return (
<div>
{state.matches("ready") && (
<button onClick={() => service.send("EVENT")} />
)}
</div>
);
}; This way, changes to (You should be able to pass |
Beta Was this translation helpful? Give feedback.
-
Hot reloading should now work as expected with the latest XState and |
Beta Was this translation helpful? Give feedback.
We are aware of the issues with hot-reloading and plan to work on fixing them. Unfortunately, this is just broken at the moment - it's because we are not capable (yet) of rehydrating the whole state into a new, incoming, machine (the new machine, the fast-refreshed one, can be completely different from the previous one after all).