0

I'm new to Redux and am having some difficulty composing a working reducer for my situation.

My current state looks like this

export const userData = {
  userID: '12345678',
  userDetails: {
    firstName: 'Joe',
    surname: 'Bloggs'
  },
  currentGames: [
    {
      gameId: 'G-00000001',
      gameSelections: [
        {
          subgameId: '',
          selection: ''
        }
      ]
    }
  ]
};

My action looks like this

function selectWinner (gameId, subgameId, selection) {
  return {
    type: SELECT_WINNER,
    gameId,
    subgameId,
    selection
  }
}

The aim is to be able to add/update the objects in the gameSelections array.

There may be more than one Object in the currentGames array also.

I've heard I should use .map but I'm not really sure how.

3 Answers 3

1

You're on the right track for using .map() to iterate over the array of objects. It also looks like your action-creator has all the necessary parameters to update your reducer state.

Your reducer can look something like this:

const userReducer = (state=userData, action) => {
    switch(action.type){
       case SELECT_WINNER:
            return {
                ...state,
                currentGames: [...state.currentGames].map((game) => {
                     if(game.gameId == action.gameId){
                        return {
                           ...game,
                           gameSelections: [...game.gameSelections].map((gameSelection) => {
                               if(gameSelection.subgameId == action.subgameId){
                                  return {
                                      ...gameSelection,
                                      selection: action.selection
                                  }
                               } else {
                                   return gameSelection
                               }
                           })
                        }
                     } else {
                         return game
                     }
                })
            }
       default:
           return state
    }
}

Kind of messy, but would get the job-done with a deeply nested state.

Sign up to request clarification or add additional context in comments.

Comments

0

Add item to array:

case'ADD_ITEM':
    return { 
        ...state,
        some_arr: [...state.some_arr, action.payload]
    }

update spicific item in array:

case 'UPDATE_ITEM':
   return { 
       ...state, 
       some_arr: state. some_arr.map(
           (item, index) => index === specific_index
              ? {...item, ...action.payload}
              : content
       )
    }

Comments

0

Deep cloning of the state is required. useful link-https://redux.js.org/recipes/structuring-reducers/immutable-update-patterns

const reducer = (state = userData, action) => {
switch (action.type) {
    case CASENAME:
        return {
            userID: state.userID,
            userDetails: {
                ...state.userdetails
            },
            currentGames: [
                {
                    gameId: action.gameId,
                    gameSelections: [
                        {
                            subgameId: action.subgameId,
                            selection: action.selection
                        }
                    ]
                }
            ]
        };
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.