简体   繁体   English

在 React Redux 中更新嵌套状态,语法有什么问题? 有没有更好的方法来编写这个减速器?

[英]Updating nested state in React Redux, what is wrong with the syntax? Is there a better method of writing this reducer?

I am trying to update this nested state called lobby.我正在尝试更新这个称为大厅的嵌套状态。 This is what 'lobby' looks like:这就是“大厅”的样子:

this.props = {
    lobby: [ 
        {
            id: 1,
            game: "Basketball",
            teams: 2,
            description: "Come!",
            filled_slots: 4,
            max_slots: 6,
            location_name: "cherry park",
            zipcode: "96024",
            active: true,
            eventDate: new Date(2018, 8, 20, 18),
            current_players_id: {
                 team1: [
                    1
                 ],
                 team2: [
                    2,
                    3,
                    4
                 ]
            }
        },
    {...},
    {...},
    ]
}

This is the update function I wrote so far to update teams, I'm having some trouble with the syntax in the return statement because I am using passed parameters.这是我迄今为止为更新团队而编写的更新函数,我在 return 语句中的语法方面遇到了一些问题,因为我使用的是传递的参数。 Any help or suggestions is much appreciated!非常感谢任何帮助或建议!

// Parameters passed to the reducer via action
gameId = 1
newTeams = { // passing newTeams to remove player from teams before adding
         team1: [
            1
         ],
         team2: [
            3,
            4
            ]
        }
 team = 'team2'
 userId = 2


// reducer
export const lobbyListReducer = ( state = intitialState, action = {}) => {
switch(action.type) {
    case JOIN_TEAM: 
        const { gameId, newTeams, team, userId } = action.payload;
        // console.log(action.payload)
        const lobbyIndex = () => {
            return state.lobby.findIndex(lobby => {
                return lobby.id === gameId
            })
        }
        // I have syntax errors below
        return {
            ...state, 
            lobby: [
                ...state.lobby, 
                state.lobby[ lobbyIndex() ]: {
                    ...state.lobby[ lobbyIndex() ],
                    state.lobby[ lobbyIndex() ].current_players_id: { 
                            ...newTeams, 
                            [team]: [ 
                                ...state.lobby[ lobbyIndex() ].current_players_id[team], 
                                userId
                            ] 
                        }
                    }
                }
            ]
        }
    default:
        return state;
}
}

What is the correct way of passing parameters when using them to reference the nested levels in an Array of Objects?使用参数引用对象数组中的嵌套级别时,传递参数的正确方法是什么?

Also is this data structure the best way to handle my data in state?这种数据结构也是处理我的状态数据的最佳方式吗?

lobby is an array, you can not use state.lobby[ lobbyIndex() ]: { to change element from an array.大厅是一个数组,您不能使用state.lobby[ lobbyIndex() ]: {来更改数组中的元素。 Also you dont need to change everything in one statement.此外,您不需要在一个语句中更改所有内容。 Do in several steps.分几个步骤做。 Build inner-most array first then next upper, then next, until you'll get final result.首先构建最内部的数组,然后是下一个上层,然后是下一个,直到您得到最终结果。

like喜欢

const lobby = state.lobby[lobbyIndex()]
const newTeam = lobby.current_players_id[team].slice()
newTeam.push(userId)
const newLobby = {...lobby, ...{current_players_id: {...lobby.current_players_id, ...{[team]: newTeam}}}}
return {...state, lobby: newLobby}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM