This skill enables the integration of XState for managing complex state machines and statecharts. It's for developers looking to implement predictable state management in their applications.
$ npx skills add https://github.com/statelyai/skills --skill xstate-v5This skill enables developers to design, implement, review, and migrate XState v5 statecharts in TypeScript applications. It provides support for modern v5 patterns, helping teams build predictable state machines and statecharts that keep code synchronized with visual diagrams. The skill is designed for developers working with complex state management who want to leverage XState's latest version for maintainable, readable state logic. It supports the full lifecycle of statechart development, from initial design through code review and migration from earlier XState versions.
Install using the provided command to add the skill.
Implementing complex state machines in applications.
Managing application states efficiently.
Creating interactive UIs with predictable behavior.
$ npx skills add https://github.com/statelyai/skills --skill xstate-v5git clone https://github.com/statelyai/skillsCopy the install command above and run it in your terminal.
Launch Claude Code, Cursor, or your preferred AI coding agent.
Use the prompt template or examples below to test the skill.
Adapt the skill to your specific use case and workflow.
Generate a state machine for [APPLICATION_NAME] using XState v5. Define states, events, and transitions for the [SPECIFIC_FEATURE] feature. Use TypeScript and include context for [DATA_SOURCE] and [USER_ACTIONS]. Ensure the machine handles edge cases like [ERROR_SCENARIO].
```typescript
import { createMachine, assign } from 'xstate-v5';
type Context = {
userId: string;
data: any;
error: string | null;
};
type Events =
| { type: 'FETCH_DATA'; userId: string }
| { type: 'DATA_RECEIVED'; data: any }
| { type: 'DATA_ERROR'; error: string }
| { type: 'RETRY' };
export const dataFetchMachine = createMachine<Context>({
id: 'dataFetch',
initial: 'idle',
context: {
userId: '',
data: null,
error: null,
},
states: {
idle: {
on: {
FETCH_DATA: {
target: 'loading',
actions: assign({ userId: (_, event) => event.userId }),
},
},
},
loading: {
invoke: {
src: 'fetchData',
onDone: { target: 'success', actions: assign({ data: (_, event) => event.data }) },
onError: { target: 'error', actions: assign({ error: (_, event) => event.data }) },
},
},
success: {
on: {
FETCH_DATA: { target: 'loading' },
},
},
error: {
on: {
RETRY: { target: 'loading' },
FETCH_DATA: { target: 'loading', actions: assign({ error: null }) },
},
},
},
});
```
**Key Features:**
- **Idempotent Transitions**: Prevents duplicate data fetches.
- **Error Handling**: Gracefully handles API failures with retry logic.
- **Type Safety**: Fully typed events, context, and states.
- **Extensible**: Easy to add new states (e.g., `cancelled`) or actions.
**Next Steps:**
1. Use `useMachine` from `@xstate/react` to integrate this machine into your React component.
2. Add a `cancel` event to handle user-initiated cancellations.
3. Log errors to a monitoring service like Sentry for debugging.Take a free 3-minute scan and get personalized AI skill recommendations.
Take free scan