以下减速器是什么意思?

时间:2019-02-25 22:18:30

标签: reactjs redux reducers

const todos = (state = [], action) => {
  switch (action.type) {
    case 'ADD_TODO':
      return [
        ...state,
        {
          id: action.id,
          text: action.text,
          completed: false
        }
      ]
    case 'TOGGLE_TODO':
      return state.map(todo =>
        todo.id === action.id ? { ...todo, completed: !todo.completed } : todo
      )
    default:
      return state
  }
}

Here.

我试图了解reducer中以下部分的含义:

[
  ...state,
  {
    id: action.id,
    text: action.text,
    completed: false
  }
]

1)... state是什么意思?

2)对象是否跟随状态附加到状态之后?

1 个答案:

答案 0 :(得分:5)

  

1)... state是什么意思?

它是将现有state数组spreading插入新数组。

  

2)对象是否跟随状态附加到状态之后?

是的,就像创建普通数组文字[{}, {}]

一样