javascript - 关于redux 连接多个reducers 即combinereducers的问题?
PHP中文网
PHP中文网 2017-04-11 10:37:55
[JavaScript讨论组]
//下面是一个reducer
import { USER_EDIT } from '../actions/actions'
const editUsers = (state = USER_EDIT , action)=>{
    switch(action.type){
        case USER_EDIT:
            return Object.assign({},action.obj)
        default:
            return state
    }
}
export default editUsers
//下面也是一个reducer
const todos = (state = [] ,action)=>{
    switch(action.type){
        case USER_ADD :
    ...
    }
}
export default todos

//下面是combineReducers
import React , {combineReducers} from 'redux'
import todos from './todos'
import editUsers from './edit'


const lists = combineReducers({
    todos,
     editUsers
})

问题来了,调试时发现只有todos这个reducer生效,editUsers这个ruducer不起作用,这是为什么?

补充 actions export const USER_ADD = 'USER_ADD';
export const USER_EDIT = 'USER_EDIT'
export const USER_DEL = 'USER_DEL'
export const USER_LIST = 'USER_LIST'
export const USER_INFO = 'USER_INFO'
export const USER_EDIT_SUB = 'USER_EDIT_SUB'

let count = 0;

export function addUser(obj){
    obj.key = count++;
    return {
        type: USER_ADD,
        obj
    }
}
export function delUser(id){
    return {
        type: USER_DEL,
        id
    }
}
export function editUser(obj){
    return {
        type: USER_EDIT,
        obj
    }
}
PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(3)
PHPz
import React from 'react'
import { combineReducers } from 'redux'
怪我咯

看着没问题啊,具体错误贴一下吧。。

大家讲道理

目测有两个错误

1.import改为这样

import React from 'react'
import { combineReducers } from 'redux'

2.editUsers这个reducer的第一个参数,state = USER_EDIT 等于一个字符串,明显错误,默认state为{}或某个对象。还有Object.assign({}, state, action.obj)

const editUsers = (state = USER_EDIT , action)=>{
    switch(action.type){
        case USER_EDIT:
            return Object.assign({}, state, action.obj)
        default:
            return state
    }
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号