Javascript 在reducer中更新byId和allid的正确方法

Javascript 在reducer中更新byId和allid的正确方法,javascript,reactjs,redux,Javascript,Reactjs,Redux,这是我的状态形状: export default { app: { loading: false, loadError: null }, search: { type: null, text: '' }, things: { byId: { }, allIds: [] } } 当我从服务器获取新的东西时,我想按照Redux docs的

这是我的状态形状:

export default {
    app: {
        loading: false,
        loadError: null
    },
    search: {
        type: null,
        text: ''
    },
    things: {
        byId: {

        },
        allIds: []
    }
}
当我从服务器获取新的
东西时,我想按照Redux docs的建议更新
东西.byId
东西.allid
。我用下面的方法做,但我觉得有更好的方法:

import { LOAD_THINGS_SUCCESS } from '../actions/actionTypes'
import initialState from './initialState'

export default function things(state = initialState.things, action) {
    switch(action.type) {
        case LOAD_THINGS_SUCCESS:
            const thingsById = {}
            action.things.forEach(thing => {
                thingsById[thing.id] = thing
            })
            return {
                ...state,
                byId: thingsById,
                allIds: action.things.map(thing => thing.id)
            }
        default:
            return state
    }
}

我使用的是
combinereducer
,上面的
reducer/things.js
就是其中之一。我关心的部分是行动。事情。对于每个…
我感觉有一种更干净/更有效的方法来做这件事。

这其实很好。如果您想使用外部实现,或者您已经在应用程序中使用了lodash,那么可以使用project@Lucas谢谢你的邀请reassurance@Carpefitzz不过,我要改变的是(我相信它更干净/高效)在单独的减缩器中分解
byId
allid
。还有
things=combinereducer({byId,allIds})
,但这不是你要问的:D@Lucas这似乎是正确的做法-谢谢!请随意把它作为一个答案贴出来,这样我就可以给你信用了