Use An XState Machine With React
import * as React from "react";
import { useMachine } from "@xstate/react";
import confirmMachine from "./confirmMachine";
import Dialog from "./dialog";
export default function App() {
const [current, send] = useMachine(confirmMachine);
const deleteAction = () => { /* ... */ };
const showDialog = current.value !== "closed";
const open = () => {
send({ type: "OPEN_DIALOG", action: deleteAction });
};
const close = () => {
send("CANCEL");
};
const confirm = () => {
send("CONFIRM");
};
return (
<div className="App">
<Dialog
message="Are you sure you want to delete something?"
handleConfirm={confirm}
handleClose={close}
showDialog={showDialog}
errorMessage={current.context.errorMessage}
/>
{/* other stuff */}
</div>
)
}Last updated