Redux 存储不创建 [英] Redux store does not create

查看:33
本文介绍了Redux 存储不创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

src/components/reducers/index.js

src/components/reducers/index.js

import {combineReducers} from 'redux';

const tasksReducer =(state=[] , action)=>{
    switch(action.type){
        case 'ADD_TASK':
            state=state.concat(action.payload);
            break;
        case 'DELETE_TASK':
            state=state.slice();
            state.splice(action.payload,1);
            break;
    }
    return state;
},
reducers=combineReducers({
    tasks:tasksReducer
});

export default reducers;

(Connect(Taskbar) 中的 mapStateToProps() 必须返回一个普通对象.而是接收到 undefined.)

(mapStateToProps() in Connect(Taskbar) must return a plain object. Instead received undefined.)

推荐答案

你不应该直接改变 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

这篇关于Redux 存储不创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆