且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Redux 存储不创建

更新时间:2023-12-06 08:52:34

你不应该直接改变 state.我没有试过你的整个代码

you should not directly mutate the state. I have not tried your whole code

import {combineReducers} from 'redux';

const tasksReducer =(state=[] , action)=>{
    switch(action.type){
        case 'ADD_TASK':
       let tempArr = [...state];
       let arr = tempArr.concat(action.payload); //action.payload is an array
           return [...arr];
            break;
        case 'DELETE_TASK':
            let tempArr= [...state];
            tempArr.splice(action.payload,1);//action.payload is index
            return [...tempArr];
            break;
        default: 
            return state;
    }
};
export default combineReducers({
    tasks:tasksReducer
});

这是一个快速演示,演示了array concat拼接

here is a quick demo demonstrating array concat and splice