I have a page containing a white t shirt and an add to basket button.
The reducer dispatches the action into a data layer
export const initialState = {
basket: [],
}
const reducer = (state, action) => {
console.log(action);
switch(action.type) {
case 'ADD_TO_BASKET':
return {
...state,
basket: [...state.basket, action.item],
};
default:
return state;
}
};
export default reducer
This is WHITETEE.js, where I pass in the values of image, name, and price.
function WHITETEE(id, title, image, price) {
const [state, dispatch] = useStateValue();
const addToBasket = () => {
dispatch({
type: 'ADD_TO_BASKET',
item: {
id: id,
title: title,
image: image,
price: price
}
})
}
return (
<div className='whitetee'>
<div className='w-tee__container'>
<div>
<Link to='/'>
<img src= {HAT} className='small__logo'></img>
</Link>
</div>
<div className='showcase__container'>
<section className='product__info'>
//When I click the button and console.log the action the value of these props
doesn't show. As I said before, the code inside 'inspect' is different, instead
of for example price='150', it's <p> tags with '150' and a class
<Product
title='WHITE TEE'
price='150'
image={Tshirt1}
/>
<p className='gradient__text' id='size'>Size</p>
<button id='add__button' onClick={addToBasket}>ADD TO CART</button>
<div className='socials'>
<img src={fb} id='fb__icon'></img>
<img src={twitter} id='twitter__icon'></img>
<img src={pintrest} id='pintrest__icon'></img>
</div>
</section>
<div id='description'>
<p className='gradient__text' id='description'>Description</p>
<p className='gradient__text' id='description2'>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris iaculis elit neque, et pharetra ex mattis nec.</p>
</div>
</div>
</div>
</div>
)
}
export default WHITETEE
When the 'add to basket' button is clicked it logs the following:
{type: 'ADD_TO_BASKET', item: {…}}
item: {id: {…}, title: {…}, image: undefined, price: undefined}
type: "ADD_TO_BASKET"
[[Prototype]]: Object
My issue is the values are empty or undefined.