stateIn() function creates a guard that evaluates to true if the machine is currently in the specified state value.
Signature
Parameters
The state value to check. Can be:
- A string for simple states (e.g.,
'idle') - A state ID string (e.g.,
'#myState') - An object for nested states (e.g.,
{ loading: 'data' })
Returns
A guard that returnstrue if the machine matches the specified state value, otherwise false.
Usage
Simple State Check
Nested State Check
Using State IDs
Conditional Actions
Parallel States
Combining with Other Guards
Multi-Step Form
State-Dependent Validation
Behavior
- State matching: Uses the same matching logic as
snapshot.matches() - State IDs: When using a state ID (prefixed with
#), checks if the machine is in that specific state node - Nested states: Supports checking nested state configurations
- Parallel states: Can check multiple parallel regions simultaneously
When to Use
UsestateIn() when you need to:
- Prevent invalid transitions: Ensure a transition only occurs from specific states
- State-dependent actions: Execute actions only in certain states
- Complex state checks: Check nested or parallel state configurations
- Guard composition: Combine with other guards for sophisticated logic
Alternative: Context-Based Guards
For simple cases, consider using context flags instead:Type Safety
See Also
- and - Combine guards with AND logic
- or - Combine guards with OR logic
- not - Invert guard results
- Guards Overview - Introduction to guards