0

为什么我不能以这种方式返回新状态(重置操作)?

export interface TodosModel {
  items: string[];
  reset: Action<TodosModel>;
}

const todos: TodosModel = {
  items: [],

  // This action does not update the state
  reset: action(() => {
    return {
      items: []
    };
  })
};

我正在尝试实现此处描述的内容:https ://github.com/ctrlplusb/easy-peasy/issues/146

工作示例:https ://codesandbox.io/s/easy-peasy-typescript-v3-vzc11

4

1 回答 1

0

为什么它不起作用有两个问题。

您未正确绑定单击操作以重置

<button onClick={() => reset()}>Reset</button>

回调传入您修改状态的状态,并且生命周期更新操作内的状态。

reset: action(state => {
    state.items = [];
  })

这是一份工作副本。https://codesandbox.io/s/easy-peasy-typescript-v3-4j9c6

于 2019-10-31T22:36:44.767 回答