freeCodeCamp/curriculum/challenges/chinese/03-front-end-libraries/redux/never-mutate-state.chinese.md

4.0 KiB
Raw Blame History

id title challengeType isRequired videoUrl localeTitle
5a24c314108439a4d4036158 Never Mutate State 6 false 从不改变国家

Description

这些最后的挑战描述了在Redux中实施状态不变性关键原则的几种方法。不可变状态意味着您永远不会直接修改状态而是返回状态的新副本。如果你随着时间的推移拍摄了一个Redux应用程序状态的快照你会看到state 1 state 2 state 3 state 4 ...等等,其中每个状态可能与最后一个状态相似,但每个状态是一个独特的数据。事实上,这种不变性提供了您可能听说过的时间旅行调试等功能。 Redux不会在其商店或减少者中主动强制执行状态不变性而责任落在程序员身上。幸运的是JavaScript尤其是ES6提供了一些有用的工具可用于强制执行状态的不变性无论是string number array还是object 。请注意字符串和数字是原始值并且本质上是不可变的。换句话说3总是3.您不能更改数字3的值。但是 arrayobject是可变的。实际上,您的状态可能包含arrayobject ,因为它们是用于表示许多类型信息的有用数据结构。

Instructions

代码编辑器中有一个storereducer器,用于管理待办事项。完成在ADD_TO_DO中写入ADD_TO_DO情况以向状态附加新的待办事项。使用标准JavaScript或ES6可以通过几种方法实现此目的。看看是否可以找到一种方法来返回一个新数组其中action.todo的项目附加到结尾。

Tests

tests:
  - text: Redux存储应该存在并使用等于代码编辑器中的<code>todos</code>数组的状态进行初始化。
    testString: 'assert((function() { const todos = [ "Go to the store", "Clean the house", "Cook dinner", "Learn to code" ]; const initialState = store.getState(); return Array.isArray(initialState) && initialState.join(",") === todos.join(","); })(), "The Redux store should exist and initialize with a state equal to the <code>todos</code> array in the code editor.");'
  - text: <code>addToDo</code>和<code>immutableReducer</code>都应该是函数。
    testString: 'assert(typeof addToDo === "function" && typeof immutableReducer === "function", "<code>addToDo</code> and <code>immutableReducer</code> both should be functions.");'
  - text: 在Redux存储上调度<code>ADD_TO_DO</code>类型的操作应该添加<code>todo</code> ,不应该改变状态。
    testString: 'assert((function() { const initialState = store.getState(); const isFrozen = DeepFreeze(initialState); store.dispatch(addToDo("__TEST__TO__DO__")); const finalState = store.getState(); const expectedState = [ "Go to the store", "Clean the house", "Cook dinner", "Learn to code", "__TEST__TO__DO__" ]; return( isFrozen && DeepEqual(finalState, expectedState)); })(), "Dispatching an action of type <code>ADD_TO_DO</code> on the Redux store should add a <code>todo</code> item and should NOT mutate state.");'

Challenge Seed

const ADD_TO_DO = 'ADD_TO_DO';

// A list of strings representing tasks to do:
const todos = [
  'Go to the store',
  'Clean the house',
  'Cook dinner',
  'Learn to code',
];

const immutableReducer = (state = todos, action) => {
  switch(action.type) {
    case ADD_TO_DO:
      // don't mutate state here or the tests will fail
      return
    default:
      return state;
  }
};

// an example todo argument would be 'Learn React',
const addToDo = (todo) => {
  return {
    type: ADD_TO_DO,
    todo
  }
}

const store = Redux.createStore(immutableReducer);

Solution

// solution required