Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
Reducer add item and update item in array of objects without mutation

Hello created reducer and wondering is this right pattern.

I am trying to:

  1. Add new item into array of objects ACTION_TYPE
  2. Update item in array of objects ACTION_TYPE_EDIT, this one actually should set charity prop and update item in array charities

Wondering if i am on right path, i don't want to mutate, so here is my code.

export const reducerCharities = (
  state: CharityReducerState,
  action: CharitiesAction
    | CharityEditAction
) => {
  switch (action.type) {
    case ACTION_TYPE_EDIT:
      return {
        ...state,
        charity: {
          ...state.charity,
          ...(action.charity || {}),
        },
        charities: [
          ...state.charities.map(item => item.id === action.charity?.id ? { ...item, ...action.charity } : item)
        ]
      }
    case ACTION_TYPE:
      return {
        ...state,
        charities: [
          ...state.charities || [],
          ...action.charities
        ]
      }
    default:
      return state
  }
}

So my question is will this part mutate state or is there better way to write this?

charities: [
          ...state.charities.map(item => item.id === action.charity?.id ? { ...item, ...action.charity } : item)
        ]

Is this right approach how to add new item into array

     {
        ...state,
        charities: [
          ...state.charities || [],
          ...action.charities
        ]
      }
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

The easiest (and since 2019 officially recommended) way of doing this would be using the official Redux Toolkit package and the createSlice method.

Within that, you can just mutate and don't have to care about manual immutable updates - the package takes over for you.

The code could look like

const charitiesSlice = ({
  name: 'charities',
  initialStae: myInitialState,
  reducers: {
    edit(state, action: PayloadAction<Charity>){
      const charity = state.charities.find(item => item.id === action.payload.id)
      if (charity) {
        Object.assign(charity, action.payload)
      }
      state.charity = action.payload
    }
  }
}
 
export const { edit } = charitiesSlice.actions
export const reducerCharities = charitiesSlice.reducer
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda