4.9 KiB
description, triggers
| description | triggers | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| Expert guidance for implementing and analyzing XState v5 state machines with TypeScript support and best practices |
|
XState v5 Expert Skill
You are an expert in XState v5, a JavaScript/TypeScript library for creating, interpreting, and executing finite state machines and statecharts using the actor model. Use this knowledge to help implement and analyze XState v5 code with precision and adherence to best practices.
Core Concepts
State Machines & Statecharts
XState implements event-driven programming through state machines and statecharts, providing predictable and robust logic handling. Always:
- Model application logic as explicit states and transitions
- Use statecharts for complex hierarchical and parallel state management
- Ensure every state transition is intentional and documented
Actor Model
XState uses the actor model for distributed, concurrent computation:
- State machine actors: Primary actors created from state machines
- Promise actors: Handle asynchronous operations
- Transition actors: Manage pure transitions
- Callback actors: Custom imperative logic
- Observable actors: Stream-based actors
Quick Start Example
import { setup, createActor, assign } from 'xstate';
const machine = setup({
types: {
context: {} as { count: number },
events: {} as { type: 'INCREMENT' } | { type: 'DECREMENT' },
},
actions: {
increment: assign({ count: ({ context }) => context.count + 1 }),
decrement: assign({ count: ({ context }) => context.count - 1 }),
},
guards: {
isPositive: ({ context }) => context.count > 0,
},
}).createMachine({
id: 'counter',
initial: 'active',
context: { count: 0 },
states: {
active: {
on: {
INCREMENT: { actions: 'increment' },
DECREMENT: {
actions: 'decrement',
guard: 'isPositive',
},
},
},
},
});
const actor = createActor(machine);
actor.subscribe((snapshot) => console.log(snapshot.context));
actor.start();
actor.send({ type: 'INCREMENT' });
📚 Reference Documentation
For detailed implementation guidance, consult the comprehensive reference documentation:
Core API Reference
Complete API documentation including:
- Machine creation (
createMachine,setup) - Actor management and lifecycle
- Actions, guards, and services
- Utility functions and type helpers
Actors Reference
Deep dive into the actor model:
- All actor types (state machine, promise, callback, transition, observable)
- Actor communication and orchestration
- Spawning vs invoking actors
- Error handling and persistence
Common Patterns
Production-ready patterns and solutions:
- Loading states with retry logic
- Form validation and submission
- Authentication flows
- Pagination, wizards, modals
- Debouncing and queue processing
TypeScript Integration
Complete TypeScript usage guide:
- Setup pattern with strong typing
- Type inference and helpers
- Generic machine factories
- Backend service types
Testing Strategies
Backend testing approaches:
- Unit testing state machines
- Using xstate-audition for actor testing
- Mocking external services
- Testing async backend operations
Best Practices
- Always use setup() for better type inference and reusable logic
- Name all actions and guards for clarity and reusability
- Use context for data, states for behavior
- Keep machines focused - one machine per logical unit
- Leverage TypeScript for compile-time safety
- Avoid inline functions in machine definitions. Used named guards and actions.
- Test with
xstate-auditionfor comprehensive coverage - Use Promise actors for asynchronous operations
Common Mistakes to Avoid
- Don't mutate context directly - always use
assign - Don't use side effects in guards - guards should be pure
- Don't overuse nested states - flatten when possible
- Don't ignore TypeScript errors - they prevent runtime issues
- Don't mix concerns - separate UI from business logic
- Don't use string events when objects provide better typing
- Don't forget error handling in async operations
- Don't use
setIntervalin machine definitions. Use delays instead.
Performance Tips
- Use
enqueueActions()for conditional actions instead of multiple transitions - Minimize context updates
- Use lazy evaluation with function updaters
- Leverage memoization for expensive computations
- Split large machines into smaller actors
Conclusion
Remember: XState excels at making complex logic predictable and maintainable. Always prioritize clarity and correctness over brevity.