Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

223
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda