我在https://github.com/tayiorbeii/egghead.io_redux_course_notes/blob/master/08-Reducer_Composition_with_Arrays.md 上关注了 Dan Abramov 的代码
我收到错误消息“第 22 行出现意外令牌”,指的是 ...todo 不认为这与 Babel 预设有关,因为 ...state 工作正常。当我在 map 函数中用 ...state 替换 ...todo 时,它返回相同的错误。
///Reducer//
export default (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 => {
if (todo.id !== action.id) {
return todo;
}
return {
...todo, //returning error
completed: !todo.completed
};
});
default:
return state;
}
}
我的调用代码:
it('handles TOGGLE_TODO', () => {
const initialState = [
{
id:0,
text: 'Learn Redux',
completed: false
},
{
id:1,
text: 'Go Shopping',
completed: false
}
];
const action = {
type: 'TOGGLE_TODO',
id: 1
}
const nextstate = reducer(initialState,action)
expect (nextstate).to.eql([
{
id:0,
text: 'Learn Redux',
completed: false
},
{
id:1,
text: 'Go Shopping',
completed: true
}
])