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

519
Vistas
React and Redux Shopping cart, add to cart action

I am working at a ReactJs and Redux shopping cart. The problem is that the item which I add to cart it is as undefined in the cart and it is not displayed.
Here is the action for add to cart:

export const addToCart = ( { item }) => {
    return (dispatch) => {
        return (
            dispatch({
                type: ADD_TO_CART,
                payload:
                { id: item?.id,
                  name: item?.name,
                  brand: item?.brand,
                  quantity: item?.quantity,
                 }
            })
        )
    }
}

Cart reducer:

const initialState = {
    cart: [],
}

export default function addToCartReducer(state=initialState, action) {
    switch(action.type){
        case ADD_TO_CART:            
            return {
                ...state,
                cart:[...state.cart, { 
                    id: action.payload.id,
                    name: action.payload.name,
                    brand: action.payload.brand,
                    quantity: action.payload.quantity
                    }]
            };
            default:
            return state    
    }
} 

And this a fragment of MyComponent:

import React, { Component } from 'react'
import { connect } from 'react-redux'
import { addToCart } from '../Actions/actions'

export class MyComponent extends Component {
 render(){
  return(
    <section>
       <button onClick = {()=>this.props.addToCart(this.props.selectedItem)}
         ADD TO CART
       </button>
    </section>
const mapStateToProps = (state) => {
    return {
        selectedItem: filterAttributesSelector(state),
    }
}

export default connect(mapStateToProps, {addToCart})(MyComponent)
  )
 }
}

If I log to the console this.props.selecteditem all the keys are defined. But if I add an item to cart and log it to the console the keys are undefined.
Could you please tell me what it is wrong?
Thank you !

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You cannot make mapStateToProps in your return , it should cause errors You need to add a mapDispatchToProps to dispatch your actions in your case the "addToCart " action. your code will be like that

import React, { Component } from 'react'
import { connect } from 'react-redux'
import { addToCart } from '../Actions/actions'

export class MyComponent extends Component {
 render(){
  return(
    <section>
       <button onClick={()=>this.props.addToCart(this.props.selectedItem)}>
         ADD TO CART
       </button>
    </section>
    )
  }
 }
const mapStateToProps = (state) => {
    return {
        selectedItem: filterAttributesSelector(state),
    }
}

const mapDispatchToProps = (dispatch) => {
  return {
    addToCart: (item) => dispatch(addToCart(item)),
  };
};
export default connect(mapStateToProps, mapDispatchToProps)(MyComponent)

** Since you send the item so no need to destruct it in your action code should be like this

export const addToCart = (item) => {
    return (dispatch) => {
        return (
            dispatch({
                type: ADD_TO_CART,
                payload:
                { id: item?.id,
                  name: item?.name,
                  brand: item?.brand,
                  quantity: item?.quantity,
                 }
            })
        )
    }
}

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