- 实现 stores 来管理应用程序状态:
- 定义可观察状态:
- 使用
@observable或makeObservable定义 store 中需要响应式跟踪的状态。 - 示例:
import { makeObservable, observable, action, computed } from 'mobx'; class TodoStore { @observable todos = []; @observable isLoading = false; @observable error = null; constructor() { makeObservable(this); } // ... actions and computed values }
- 使用
- 利用计算值(
@computed)处理派生状态:- 对于可以从现有可观察状态派生出的数据,使用
@computed属性。它们是惰性求值的,并且只有在依赖的可观察数据发生变化时才会重新计算。 - 示例:
class TodoStore { // ... @computed get completedTodosCount() { return this.todos.filter(todo => todo.completed).length; } @computed get uncompletedTodos() { return this.todos.filter(todo => !todo.completed); } }
- 对于可以从现有可观察状态派生出的数据,使用
- 使用动作(
@action)修改可观察状态:- 所有修改 MobX store 可观察状态的函数都应该用
@action标记。这有助于 MobX 批量更新,提高性能,并使调试更容易。 - 示例:
class TodoStore { // ... @action addTodo(text: string) { this.todos.push({ id: Date.now(), text, completed: false }); } @action toggleTodo(id: number) { const todo = this.todos.find(t => t.id === id); if (todo) { todo.completed = !todo.completed; } } }
- 所有修改 MobX store 可观察状态的函数都应该用
- 在异步 actions 中实现适当的错误处理:
- 对于异步操作(如 API 调用),应在 action 中处理加载状态、成功和失败情况,并捕获错误。
- 示例:
class TodoStore { // ... @action async fetchTodos() { this.isLoading = true; this.error = null; try { // 模拟 API 调用 const response = await new Promise(resolve => setTimeout(() => resolve([{ id: 1, text: 'Learn MobX', completed: false }]), 1000) ); this.todos = response; } catch (e) { this.error = e; } finally { this.isLoading = false; } } }
- Store 的组合:
- 对于大型应用,可以将多个相关的 store 组合成一个根 store,方便统一管理和访问。
- 示例:
// RootStore.ts import { makeObservable, observable } from 'mobx'; import { TodoStore } from './TodoStore'; import { UserStore } from './UserStore'; export class RootStore { @observable todoStore: TodoStore; @observable userStore: UserStore; constructor() { makeObservable(this); this.todoStore = new TodoStore(); this.userStore = new UserStore(); } }
- 定义可观察状态:
Mobx Store Implementation
实现 MobX stores 进行应用程序状态管理的指导原则
Mobx Store Implementation by liaosw97 · 85a99fd
npx skillmds@latest add liaosw97/mobx-store-implementation File contents
---name: mobx-store-implementationdescription: 实现 MobX stores 进行应用程序状态管理的指导原则---- **实现 stores 来管理应用程序状态**: - **定义可观察状态**: - 使用 `@observable` 或 `makeObservable` 定义 store 中需要响应式跟踪的状态。 - 示例: ```typescript import { makeObservable, observable, action, computed } from 'mobx'; class TodoStore { @observable todos = []; @observable isLoading = false; @observable error = null; constructor() { makeObservable(this); } // ... actions and computed values } ``` - **利用计算值(`@computed`)处理派生状态**: - 对于可以从现有可观察状态派生出的数据,使用 `@computed` 属性。它们是惰性求值的,并且只有在依赖的可观察数据发生变化时才会重新计算。 - 示例: ```typescript class TodoStore { // ... @computed get completedTodosCount() { return this.todos.filter(todo => todo.completed).length; } @computed get uncompletedTodos() { return this.todos.filter(todo => !todo.completed); } } ``` - **使用动作(`@action`)修改可观察状态**: - 所有修改 MobX store 可观察状态的函数都应该用 `@action` 标记。这有助于 MobX 批量更新,提高性能,并使调试更容易。 - 示例: ```typescript class TodoStore { // ... @action addTodo(text: string) { this.todos.push({ id: Date.now(), text, completed: false }); } @action toggleTodo(id: number) { const todo = this.todos.find(t => t.id === id); if (todo) { todo.completed = !todo.completed; } } } ``` - **在异步 actions 中实现适当的错误处理**: - 对于异步操作(如 API 调用),应在 action 中处理加载状态、成功和失败情况,并捕获错误。 - 示例: ```typescript class TodoStore { // ... @action async fetchTodos() { this.isLoading = true; this.error = null; try { // 模拟 API 调用 const response = await new Promise(resolve => setTimeout(() => resolve([{ id: 1, text: 'Learn MobX', completed: false }]), 1000) ); this.todos = response; } catch (e) { this.error = e; } finally { this.isLoading = false; } } } ``` - **Store 的组合**: - 对于大型应用,可以将多个相关的 store 组合成一个根 store,方便统一管理和访问。 - 示例: ```typescript // RootStore.ts import { makeObservable, observable } from 'mobx'; import { TodoStore } from './TodoStore'; import { UserStore } from './UserStore'; export class RootStore { @observable todoStore: TodoStore; @observable userStore: UserStore; constructor() { makeObservable(this); this.todoStore = new TodoStore(); this.userStore = new UserStore(); } } ```
liaosw97/awesome-rules-skills/tree/main/plugins/mobx/skills/mobx-store-implementation commit 85a99fdf2c
Frequently asked questions
Run npx skillmds@latest add liaosw97/mobx-store-implementation in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
实现 MobX stores 进行应用程序状态管理的指导原则 It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Capability flags: docs only. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
liaosw97 (@liaosw97) published this skill. Their other Agent Skills are listed on their SkillMD profile.