reducer 中的 React-redux Spread 运算符返回错误 "unexpected token"
React-redux Spread operator in reducer returning error "unexpected token"
我遵循了 Dan Abramov 在 https://github.com/tayiorbeii/egghead.io_redux_course_notes/blob/master/08-Reducer_Composition_with_Arrays.md
的代码
我收到关于 ...todo 的错误消息 "Unexpected token at line 22"
不认为这与 Babel 预设有关,因为 ...state 工作正常。当我在 map 函数中用 ...state 替换 ...todo 时,它 returns 同样的错误。
///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
}
])
实际上是关于预设的。
数组展开是 ES2015 标准的一部分,你在这里使用它
return [...state,
{
id:action.id,
text: action.text,
completed:false
}
];
不过,这里
return {
...todo, //returning error
completed: !todo.completed
};
您使用 object spread which is not part of the standard, but a stage 2 proposal。
您需要在 Babel 中启用对此提议的支持:https://babeljs.io/docs/plugins/transform-object-rest-spread/ or desugar it into Object.assign
calls (see this part of the proposal)
我遵循了 Dan Abramov 在 https://github.com/tayiorbeii/egghead.io_redux_course_notes/blob/master/08-Reducer_Composition_with_Arrays.md
的代码我收到关于 ...todo 的错误消息 "Unexpected token at line 22" 不认为这与 Babel 预设有关,因为 ...state 工作正常。当我在 map 函数中用 ...state 替换 ...todo 时,它 returns 同样的错误。
///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
}
])
实际上是关于预设的。
数组展开是 ES2015 标准的一部分,你在这里使用它
return [...state,
{
id:action.id,
text: action.text,
completed:false
}
];
不过,这里
return {
...todo, //returning error
completed: !todo.completed
};
您使用 object spread which is not part of the standard, but a stage 2 proposal。
您需要在 Babel 中启用对此提议的支持:https://babeljs.io/docs/plugins/transform-object-rest-spread/ or desugar it into Object.assign
calls (see this part of the proposal)