npx create-react-app react-state-machines
npm install xstate @xstate/react
.
src/Machines/bookingMachine.js:
import { createMachine } from 'xstate';
const bookingMachine = createMachine({
id: 'buy plane tickets',
initial: 'initial',
states: {
initial: {
on: {
START: 'search'
}
},
search: {
on: {
CONTIUNE: 'passengers',
CANCEL: 'initial'
}
},
passengers: {
on: {
DONE: 'tickets',
CANCEL: 'initial'
}
},
tickets: {
on: {
FINISH: 'initial'
}
},
}
})
export default bookingMachine
src/Components/BaseLayout.js:
import React from 'react';
import { useMachine } from '@xstate/react';
import bookingMachine from '../Machines/bookingMachine';
export const BaseLayout = ()=> {
const [state,send] = useMachine(bookingMachine)
console.log('machine', state)
return (
<div>State Machine</div>
)
}
src/App.js:
import { BaseLayout } from './Components/BaseLayout';
import './App.css';
function App() {
return (
<div className="App">
<BaseLayout />
</div>
);
}
export default App;
¿Quieres ver más aportes, preguntas y respuestas de la comunidad?