Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

517
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!