[XState + Typescript] Type XState
typescript type XState
2023-09-14 09:00:44 时间
import { assign, createMachine } from "xstate";
type FetchStates =
| {
value: "idle";
context: FetchContext & {
results: [];
message: "";
};
}
| { value: "pending"; context: FetchContext }
| {
value: "failed";
context: FetchContext & {
results: [];
message: "";
};
}
| {
value: "successful";
context: FetchContext & {
results: [];
message: "";
};
};
type FetchMachineEvents =
| { type: "FETCH" }
| { type: "RESOLVE"; results: any[] }
| { type: "REJECT"; message: string };
interface FetchContext {
results: any[];
message: string;
}
export default createMachine<FetchContext, FetchMachineEvents, FetchStates>(
{
predictableActionArguments: true,
id: "fetch",
initial: "idle",
context: {
results: [],
message: "",
},
states: {
idle: {
on: {
FETCH: "pending",
},
},
pending: {
entry: ["fetchData"],
on: {
RESOLVE: { target: "successful", actions: ["setResults"] },
REJECT: { target: "failed", actions: ["setMessage"] },
},
},
failed: {
on: {
FETCH: "pending",
},
},
successful: {
on: {
FETCH: "pending",
},
},
},
},
{
actions: {
setResults: assign((ctx, event: any) => ({
results: event.results,
})),
setMessage: assign((ctx, event: any) => ({
message: event.message,
})),
},
}
);
相关文章
- java设置content type_Response Content Type设置[通俗易懂]
- Getting Started: Ionic + Capacitor + React + Typescript + Android
- Typescript 4.9重点特性探索
- Flow 与 Typescript:哪个更适合你的项目?
- TypeScript 中的 any、unknown、never 和 void
- TypeScript泛型应用
- TypeScript 4.8 发布!重点新特性解读
- 软件开发入门教程网站之TypeScript Array(数组)
- 用TodoList实例告诉你怎么在项目中使用TypeScript
- (十四)与 TypeScript 集成
- TypeScript 中提升幸福感的 10 个高级技巧
- 调查:React 仍然是使用最广泛的前端框架,TypeScript 是优先选项
- 【错误记录】Kotlin 编译报错 ( Type mismatch: inferred type is String? but String was expected )
- ORA-22870: ALTER TYPE with REPLACE option a non-object type ORACLE 报错 故障修复 远程处理
- 创建 Oracle 中的Type: 指南(oracle创建type)
- Oracle中定义Type的概念及其用法(oracle定义type)
- Linux下使用Type命令掌握文件说明(linux中 type)