简体   繁体   English

购物车按钮反应的问题

[英]a problem with the shopping cart button in react

I'm trying to make a button where every time I click it adds the item to the cart, but the problem is that when I click nothing happens, as if it were not even a button:我正在尝试制作一个按钮,每次我点击它都会将商品添加到购物车,但问题是当我点击时什么也没有发生,好像它甚至不是一个按钮:

Product.js :产品.js :

import React from 'react'
import './Product.css'
import {useStateValue} from './StateProvider'

function Products({id,title,image,price,rating}) {
const [state,dispatch] = useStateValue();
const addToBasket = () => {
    dispatch({
        type: 'ADD_TO_BASKET',
        item:{
            id: id,
            title: title,
            image: image, 
            price: price,
            rating: rating,  
        },
    });
   };
  return (
    <div className="product">
        <div className="product_info">
            <p>{title}</p>
            <p className="product_price">
                <small>$</small>
                <strong>{price}</strong>
            </p>
            <div className="product_rating">
                {Array(rating).fill().map((_,i) => (
                    <p>⭐</p>
                ))}
            </div>
        </div> 

        <img src={image} alt="" /> 
        <button onClick={addToBasket}>Add to Basket</button> 
    </div>
   );
   }
  export default Products;

Reducer.js :减速器.js:

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;

Index.js :索引.js :

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { StateProvider } from './StateProvider';
import reducer, {initialState} from './reducer'

ReactDOM.render(
<React.StrictMode>
<StateProvider initialState={initialState} reducer={reducer}>
<App />
</StateProvider>
</React.StrictMode>,
document.getElementById('root')
);
reportWebVitals();

Header.js (where I put the basket logo): Header.js(我放篮子标志的地方):

import React from 'react';
import './Header.css';
import SearchIcon from '@material-ui/icons/Search'
import ShopppingBasketIcon from '@material-ui/icons/ShoppingBasket'
import {Link} from 'react-router-dom'
import { useStateValue } from "./StateProvider"

function Header() {
const [{basket},dispatch] = useStateValue();
return (
    <div className="header"> 
    <Link to="/">
    <img className="header__logo" src="http://pngimg.com/uploads/amazon/amazon_PNG11.png"/>
    </Link>       
    <div className="header__search">
    <input className="header__searchInput" type="text"/> 
    <SearchIcon className="header__searchIcon"/>
    </div>

    <div className="header__nav">
        <Link to="/checkout">
        <div className="header__optionBasket">
            <ShopppingBasketIcon/>
            <span className="header__optionLineTwo
            header__basketCount">{basket?.lenght}</span>
        </div>
        </Link>
    </div>
    </div>
)
}
export default Header

therefore, where I find the problem is that when I click "add to cart" it does not click anything, as if the button did not exist and does not take any action..因此,我发现问题的地方在于,当我单击“添加到购物车”时,它没有单击任何内容,就好像该按钮不存在并且不采取任何操作一样..

try preventing default behaviour like so尝试防止这样的默认行为

const addToBasket = (e) => {
e.preventDefault();
dispatch({
    type: 'ADD_TO_BASKET',
    item:{
        id: id,
        title: title,
        image: image, 
        price: price,
        rating: rating,  
    },
});
};

and you can also try setting a type on the button你也可以尝试在按钮上设置一个类型

<button type="button" onClick={addToBasket}>Add to Basket</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM