一键导入
xstate
XState v5 state machines and statecharts for JavaScript/TypeScript
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
XState v5 state machines and statecharts for JavaScript/TypeScript
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Prepend session handoff to DaysActivity.md
Initialize session with context briefing
File production feedback about a zgent's behavior. Routes to CSO for diagnosis and artifact tuning. Usage: /zgent-feedback <zgent> <description>
Run A/B tests comparing convention variants to measure compliance improvements
Archive yesterday's DaysActivity and create fresh file for today
DaysActivity.md formatting conventions. Use when writing handoff entries, hourly summaries, or any content destined for DaysActivity.md.
| name | xstate |
| description | XState v5 state machines and statecharts for JavaScript/TypeScript |
| context | fork |
| allowed-tools | Read, Bash, Glob, Grep |
Actor-based state management & orchestration for complex app logic.
Repository: statelyai/xstate Language: TypeScript Stars: 29,364 License: MIT License
import { createMachine, createActor, assign } from 'xstate';
const toggleMachine = createMachine({
id: 'toggle',
initial: 'inactive',
context: { count: 0 },
states: {
inactive: {
on: { TOGGLE: { target: 'active' } }
},
active: {
entry: assign({ count: ({ context }) => context.count + 1 }),
on: { TOGGLE: { target: 'inactive' } }
}
}
});
const actor = createActor(toggleMachine);
actor.subscribe((state) => console.log(state.value, state.context));
actor.start();
actor.send({ type: 'TOGGLE' });
import { setup, 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 })
}
}).createMachine({
initial: 'active',
context: { count: 0 },
states: {
active: {
on: {
INCREMENT: { actions: 'increment' },
DECREMENT: { actions: 'decrement' }
}
}
}
});
const lightMachine = createMachine({
id: 'light',
initial: 'green',
states: {
green: { on: { TIMER: 'yellow' } },
yellow: { on: { TIMER: 'red' } },
red: {
on: { TIMER: 'green' },
initial: 'walk',
states: {
walk: { on: { PED_TIMER: 'wait' } },
wait: { on: { PED_TIMER: 'stop' } },
stop: {}
}
}
}
});
const wordMachine = createMachine({
id: 'word',
type: 'parallel',
states: {
bold: {
initial: 'off',
states: {
on: { on: { TOGGLE_BOLD: 'off' } },
off: { on: { TOGGLE_BOLD: 'on' } }
}
},
italics: {
initial: 'off',
states: {
on: { on: { TOGGLE_ITALICS: 'off' } },
off: { on: { TOGGLE_ITALICS: 'on' } }
}
}
}
});
import { useActor } from '@xstate/react';
function ToggleComponent() {
const [state, send] = useActor(toggleMachine);
return (
<div>
<p>State: {state.value}</p>
<p>Count: {state.context.count}</p>
<button onClick={() => send({ type: 'TOGGLE' })}>
Toggle
</button>
</div>
);
}
import { createStore } from '@xstate/store';
const store = createStore({
context: { count: 0 },
on: {
inc: (ctx) => ({ count: ctx.count + 1 }),
dec: (ctx) => ({ count: ctx.count - 1 })
}
});
store.subscribe((snapshot) => {
console.log(snapshot.context.count);
});
store.trigger.inc(); // count = 1
import { useSelector } from '@xstate/store-react';
function Counter() {
const count = useSelector(store, (state) => state.context.count);
return (
<div>
<span>{count}</span>
<button onClick={() => store.trigger.inc()}>+</button>
<button onClick={() => store.trigger.dec()}>-</button>
</div>
);
}
const machine = setup({
guards: {
isPositive: ({ context }) => context.count > 0,
canIncrement: ({ context }) => context.count < 10
},
actions: {
logCount: ({ context }) => console.log('Count:', context.count)
}
}).createMachine({
context: { count: 0 },
states: {
active: {
on: {
INCREMENT: {
guard: 'canIncrement',
actions: ['increment', 'logCount']
},
DECREMENT: {
guard: 'isPositive',
actions: ['decrement', 'logCount']
}
}
}
}
});
const machine = createMachine({
id: 'app',
initial: 'home',
states: {
home: { id: 'home', route: {} },
dashboard: {
initial: 'overview',
states: {
overview: { id: 'overview', route: {} },
settings: {
id: 'settings',
route: {
guard: ({ context }) => context.role === 'admin'
}
}
}
}
}
});
// Route directly to deeply nested state
actor.send({ type: 'xstate.route', to: '#settings' });
references/README.md - Complete documentation with examples, templates, and getting started guidereferences/issues.md - Recent GitHub issues including bugs, feature requests, and known problemsreferences/releases.md - Detailed release notes with new features and fixesreferences/file_structure.md - Repository structure showing packages, examples, and source organizationreferences/README.md for core functions and methodsreferences/issues.md for known problems and workaroundsreferences/releases.md for version upgrade guidancexstate - Core state machine library@xstate/react - React hooks and utilities@xstate/vue - Vue composition functions@xstate/svelte - Svelte utilities@xstate/store - Simple state management@statelyai/inspect - Development tools