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

118
Visualizações
Redux adding new item to state

I have two cases in my reducer:

case GET_CODE:
    return {
      ...state,
      list:[...state.list,{code: action.payload.product.Code}]
    } 
case WEIGHT:
      return {
        ...state,
        list:[...state.list,{weight:action.payload}]
      }  

My state is looking like this:

   list: [
          { weight: ''},
          {weight: '100'},
          {weight: '200'},      
          {code: '63'},
          {code: '64'}   
         ]

What i would like to have is something like:

   list: [
          {weight: ''},
          {weight: '100', code:'63'},   
          {weight: '200', code:'64'}  
        ]

My initial state is list:[] . First empty value of weight is from initial render i think.I dispatch first WEIGHT then GET_CODE action.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should write some sort of helper function that creates the object for you, or the code/weight case should find the object in list it should correspond to.

For the helper function:

const createObject = (code, weight) => {
  const obj = {code: code.product.Code, weight: weight}
  dispatch({type: 'ADD_TO_LIST', payload: obj});
} 

// In your reducer
case ADD_TO_LIST:
  return {
    ...state,
    list: [
      ...state.list,
      action.payload
    ]
  }

Or just use ADD_TO_LIST case and create the object in the reducer

i.e.

case ADD_TO_LIST:
  return {
    ...state,
    list: [
      ...state.list,
      {code: action.payload.code.product.Code, weight: action.payload.weight}
    ]
  }
about 4 years ago · Juan Pablo Isaza 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