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

218
Visualizações
What should be the correct inital value for redux state

I wanted to create 'add to cart' functionality using Redux, so I have defined a reducer function which accepts a state and action, where state is provided a default value.

export const defaultValue= {
  products: [
    {
      id: "",
      title: "",
      description: "",
      image: "",
      price: 0,
      amount: 0,
    },
  ]
};

export const manageCart = (
  state = defaultValue,
  action: { type: string; payload: any}
) => {
  switch (action.type) {
    case actionConstants.ADD_TO_CARTS:
      {
       // ADD to card
      }
  break;
    case actionConstants.REMOVE_FROM_CART: {
      // Remove from card     
     }
 default:
      return state;
}
}

enter image description here So by default I'm seeing this blank product on my cart which is messing up my logic moving further. Since it is mandatory to provide initial value to the state, what should i do differently to get rid of this issue.

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

0

So by default I'm seeing this blank product on my cart which is messing up my logic moving further.

Because you are passing that blank product in the default state!

Just pass an empty array instead

export const defaultValue= {
  products: []
};
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