简体   繁体   English

ReactJs handleAddProduct 不是 onClick 的函数

[英]ReactJs handleAddProduct is not a function at onClick

I'm finishing a page with shopping cart.我正在用购物车完成一个页面。 I have a function that adds products to the cart and that works in other sections of the page perfectly.我有一个功能可以将产品添加到购物车中,并且可以在页面的其他部分完美运行。 The problem is that on the home page the function tells me:问题是在主页上该功能告诉我:

handleAddProduct is not a function at onClick . handleAddProduct不是onClick的函数。

Here I leave the components that are causing problems在这里,我留下导致问题的组件

App.js应用程序.js

const [cartItems, setCartItems] = useState([]);

  const handleAddProduct = (product) => {
    toast('✔️ Producto añadido')
    console.log('Producto seleccionado')
    const ProductExist = cartItems.find((item) => item.id === product.id);
    if(ProductExist){
      setCartItems(
        cartItems.map((item)=>
        item.id === product.id
        ? {...ProductExist, quantity: ProductExist.quantity + 1}
        : item
        )
      );
    } else {
      setCartItems([...cartItems, {...product, quantity: 1}]);
    }
    
  };

  ...

  // Here I am passing the handleAddProduct function as a prop to the 'Inicio.jsx' component
  <Route path='/' exact element={<Inicio handleAddProduct={handleAddProduct} />}/>

Inicio.jsx Inicio.jsx

Here I am importing the handleAddProduct function as a prop to the 'Inicio.jsx' component and then passing it back as a prop to the Cards.jsx component在这里,我将 handleAddProduct 函数作为道具导入到“Inicio.jsx”组件,然后将其作为道具传递回 Cards.jsx 组件

const Inicio = ({ handleAddProduct }) => {
  return (
    <>
      <section className="new__product__section">
        <h2 className="title__section__card">Nuevos Productos</h2>
        <Card items={CardItemsHero} handleAddProduct={handleAddProduct} />
        <div className="btn__link__container">
          <Button color="primary" variant="contained" size="large">
            <Link to={'/catalogo'}>
              Ver Catálogo
            </Link>
          </Button>
        </div>
      </section>

    ...

Card.jsx卡片.jsx

HERE IS THE PROBLEM 1这是问题1

const Card = (props, { handleAddProduct }) => {
  return (
    <>
      <div className='card__section'>
        {props.items.map(item => {
          return(
            <div className="product__card" key={item.id}>
              <div className="product__tumb">
                <img src={require('../../assets/' + item.image + '.png')} alt={item.title} />
              </div>
              <div className="product__details">
                <span className="product__category">{item.type}</span>
                <h3>{item.title}</h3>
                <div className="product__bottom__details">
                  <div className="product__price">{'$ ' + item.price + '.00'}</div>
                  <div className="product__links">
                    <Link to={'/' + item.id}><InfoIcon /></Link>
                    <span onClick={()=>handleAddProduct(item)}>
                      <ShoppingCartIcon/>
                    </span>
                  </div>
                </div>
              </div>
            </div>
          )
        })}
      </div>
    </>
  )
}

CLARIFICATIONS: the page works perfectly.澄清:页面完美运行。 I am passing the same function in the same way to other components within another component, even with the same <span> line, and the products are added without any problem.我以相同的方式将相同的功能传递给另一个组件中的其他组件,即使使用相同的<span>行,并且添加产品没有任何问题。 I think it's an error when importing the props in Card.jsx but I can't think of what it could be because if I just put {props, handleAddProducts} the page stops working我认为在 Card.jsx 中导入道具时出现错误,但我想不出它可能是什么,因为如果我只是放置{props, handleAddProducts}页面将停止工作

Here is the repo: https://github.com/LuksFay/Fabianesi-page这是回购: https ://github.com/LuksFay/Fabianesi-page

The issue is with the Card component.问题出在Card组件上。 React components receive only a single argument, the props object. React 组件只接收一个参数,即props对象。 In the code the Card component is attempting to destructure handleAddProduct from an undefined argument.在代码中, Card组件试图从未定义的参数中解构handleAddProduct

const Card = (props, {handleAddProduct}) => {

Destructure handleAddProduct from the first argument, the props object.从第一个参数props对象中handleAddProduct

Example:例子:

const Card = ({ handleAddProduct, items }) => { // <-- destructure all from props
  return (
    <>
      <div className="card__section">
        {items.map((item) => {
          return (
            <div className="product__card" key={item.id}>
              <div className="product__tumb">
                <img
                  src={require("../../assets/" + item.image + ".png")}
                  alt={item.title}
                />
              </div>
              <div className="product__details">
                <span className="product__category">{item.type}</span>
                <h3>{item.title}</h3>
                <div className="product__bottom__details">
                  <div className="product__price">
                    {"$ " + item.price + ".00"}
                  </div>
                  <div className="product__links">
                    <Link to={"/" + item.id}>
                      <InfoIcon />
                    </Link>
                    <span
                      onClick={() => {
                        handleAddProduct(item);
                      }}
                    >
                      <ShoppingCartIcon />
                    </span>
                  </div>
                </div>
              </div>
            </div>
          );
        })}
      </div>
    </>
  );
};

Your Card.jsx file props declaration is not seems to be correct.您的 Card.jsx 文件道具声明似乎不正确。 Change this like bellow.像下面这样改变。

const Card = (props) => {

  const { handleAddProduct } =  props

  return (
    <>
       [...your code]
    </>
  )
}

Just catch only props and declare the function on the next line like this.只捕获道具并像这样在下一行声明函数。 You cannot catch props and another value inside props together.您不能将 props 和 props 中的另一个值一起捕获。 you can also declare another props items like the same您还可以声明另一个类似的道具项目

const { handleAddProduct, items } =  props

You can directly use the state [items] and function [handleAddProduct] on your jsx inside Card.jsx您可以在 Card.jsx 中的 jsx 上直接使用状态 [items] 和函数 [handleAddProduct]

Just Update this Card.jsx file: Now Working只需更新此 Card.jsx 文件:现在工作

you just take only one Concept: props or {items,handleAddProduct}你只需要一个概念: props 或 {items,handleAddProduct}

const Card = (props) => {
  return (
    <>
      <div className="card__section">
        {props.items.map((item) => {
          return (
            <div className="product__card" key={item.id}>
              <div className="product__tumb">
                <img
                  src={require('../../assets/' + item.image + '.png')}
                  alt={item.title}
                />
              </div>
              <div className="product__details">
                <span className="product__category">{item.type}</span>
                <h3>{item.title}</h3>
                <div className="product__bottom__details">
                  <div className="product__price">
                    {'$ ' + item.price + '.00'}
                  </div>
                  <div className="product__links">
                    <Link to={'/' + item.id}>
                      <InfoIcon />
                    </Link>
                    <span
                      onClick={() => {
                        props.handleAddProduct(item);
                      }}
                    >
                      <ShoppingCartIcon />
                    </span>
                  </div>
                </div>
              </div>
            </div>
          );
        })}
      </div>
    </>
  );
};

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

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