switch case语句react redux出现语法错误



在我的代码中,action.payload指向todo索引,所以如果todo的索引与触发事件的索引相同,那么我的REMOVE_todo案例应该过滤掉它。但是,我到处都有语法错误。我把包装搞砸了,甚至尝试从头开始重写,但无法消除这些语法错误。有人看到我在这里丢失的东西了吗??如果需要更多信息,请告诉我。谢谢大家!

import { ADD_TODO } from '../constants/action-types'; 
import { REMOVE_TODO } from '../constants/action-types';
const initialState = {
todos: []
};
const rootReducer = (state = initialState, action) => {
switch (action.type) {
case ADD_TODO:
return {
...state,
todos: [...state.todos, action.payload]
}
case REMOVE_TODO:
return {
...state,
todos: [...state.todos.filter((todo, i) => {i  !== action.payload})]
}
default:
return state;
}
}
export default rootReducer;

您错过了在filter函数内返回值

看看演示。

const initialState = {
todos: [1,2,3,7,8,5]
};
const rootReducer = (state = initialState, action) => {
switch (action.type) {
case 'ADD_TODO':
return {
...state,
todos: [...state.todos, action.payload]
}
case 'REMOVE_TODO':
return {
...state,
todos: [...state.todos.filter((todo, i) => i  !== action.payload)]
}
default:
return state;
}
}
let op = rootReducer({todos:[1,2,3,4,5,6]},{type:'REMOVE_TODO',payload:1});
console.log(op)

原来我只是从过滤器方法返回一个对象。todos: [...state.todos.filter((todo, i) => {i !== action.payload})]只是去掉了箭头函数返回中的花括号,现在一切都很好。

相关内容

  • 没有找到相关文章

最新更新