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

303
Visualizações
How to save an object in redux?

I build an app in React with Redux and I try to send to my state an object and I try to save it in 'thisUser' but I don't know how to write that 'return' because mine doesn't work.

My Redux state:

const initialState = {
    thisUser: {}
}

export function usersReducer(state = initialState, action) {
    switch (action.type) {
        case 'users/addUser':
            return { ...state, thisUser: { ...state.thisUser, ...action.payload} }  //the problem
        default:
            return state
    }
}

Dispatch method:

dispatch({ type: "users/addUser", payload: new_user });

Can you tell me how to write that return, please?

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

0

If you want to append new user then why are you using object type. You should use Array Type thisUser.

const initialState = {
  thisUser: []
}

export function usersReducer(state = initialState, action) {
   switch (action.type) {
      case 'users/addUser':
          return { ...state, thisUser: [ ...state.thisUser,action.payload ] }  
      default:
          return state
  }
}

Or

If you want to save only single user object then change only that line in your code:

   return { ...state, thisUser: action.payload }  
about 4 years ago · Juan Pablo Isaza Relatório

0

It's better to use an array type for if you have a list of users .

If you have a case when you need to use an object just change the brackets [ ] on my code to curly braces { } .

const initialState = {
    thisUser: [],
}

export function usersReducer(state = initialState, action) {
    switch (action.type) {
        case 'users/addUser':
            return { ...state, thisUser: [ ...state.thisUser, ...action.payload]}
        default:
            return state
    }
}  
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