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 !