编写 Actions(动作)

使用 MobX 来编写动作是很直观的。 只是简单地创建、更改或删除数据,MobX 将确保变化会由 store 和依赖于这些数据的组件捕获。 在前一章节中我们所创建的 store 基础上,动作可以如此简单:

  1. var todo = todoStore.createTodo();
  2. todo.task = "make coffee";

这足以创建一个待办事项,提交到服务器,并相应地更新我们的用户界面。

何时使用动作?

动作只应该在修改状态的函数上使用。 仅执行查找,过滤等操作的函数应该标记为动作,以允许 MobX 追踪它们的调用。

异步动作

Writing asynchronous actions is pretty simple as well. You can use observable data structures as a promise. This is what happens with the isLoading property in the todoStore for example: 编写异步动作同样非常的简单。 可以使用 observable 数据结构作为 promise。 示例中 todoStoreisLoading 属性就是这样的:

  1. // ...
  2. this.isLoading = true;
  3. this.transportLayer.fetchTodos().then(fetchedTodos => {
  4. fetchedTodos.forEach(json => this.updateTodoFromServer(json));
  5. this.isLoading = false;
  6. });
  7. // ...

异步动作完成后,只是更新了数据,视图也会更新。 React 组件的 render 函数 可以变得如此简单:

  1. import {observer} from "mobx-react";
  2. var TodoOverview = observer(function(props) {
  3. var todoStore = props.todoStore;
  4. if (todoStore.isLoading) {
  5. return <div>Loading...</div>;
  6. } else {
  7. return <div>{
  8. todoStore.todos.map(todo => <TodoItem key={todo.id} todo={todo} />)
  9. }</div>
  10. }
  11. });

上面的 TodoOverview 组件每当 isLoading 变化时,或 isLoading 为 true 且 todos 改变时就会更新。 注意,我们可以将 todoStore.isLoading 替换为 todoStore.todos.length 。 结果是相同的。