简体   繁体   English

为什么我的 onclick function 在第一次点击时不起作用?

[英]why is my onclick function not working on the first click?

I'm creating a sneaker store where I have an add to cart function,我正在创建一家运动鞋店,我在其中添加了购物车 function,

first, when the add to cart is clicked it updates the size that's selected second it adds the changed item,首先,当单击添加到购物车时,它会更新所选的尺寸,其次它会添加已更改的项目,

object into the cart State the problem is that on the first click there is no object returned but object 放入购物车 State 问题是在第一次点击时没有返回 object 但

every time after the first click it returns the data.每次第一次点击后,它都会返回数据。

this is the function that updates the size:这是更新大小的 function:

 const updateSelectedSize=(item,selectedSize)=>{
        item.size=selectedSize;
        return item;
    
      }

here is the function that I use to add the updated item to the cart:这是我用来将更新后的商品添加到购物车的 function:

const handleAddtoCart=(item,selectedSize)=>{


 const  updatedItem=updateSelectedSize(item,selectedSize);
 

 let  newitemArray=[...cart,updatedItem]
  setcart(newitemArray)


console.log(cart);
 

 }

here is the component that executes the function on click:这是在单击时执行 function 的组件:
<i onClick={()=>{handleAddtoCart(product,selectedSize)}}className="fa-solid fa-cart-arrow-down"></i>

here is the console.log of the first click:这是第一次点击的 console.log: 在此处输入图像描述

and here's the console log of the second click:这是第二次点击的控制台日志: 在此处输入图像描述

as you can see nothing happens on the first click I'm thinking this is has something to do with the useState cart variable and the spread operator the initial value for it is:正如您在第一次点击时看到的那样,我认为这与 useState 购物车变量和传播运算符有关,它的初始值为:

 const [cart, setcart] = useState('');

I've tried to set the value to different things such as an empty array but it always gives the same result.我试图将值设置为不同的东西,例如空数组,但它总是给出相同的结果。

Because you write your console.log(cart) inside the handleAddtoCart and your state not updated yet.因为您在handleAddtoCart中编写了console.log(cart)而您的 state 尚未更新。

if you move console.log(card) outside of handleAddtoCart function, you can see the result is work perfectly.如果将 console.log(card) 移到handleAddtoCart function 之外,您可以看到结果完美无缺。

And for empty array it is better to use对于空数组,最好使用

✅  const [cart, setcart] = useState([]);

instead of this one而不是这个

❌  const [cart, setcart] = useState("");

Full code is here完整代码在这里

export default function App() {
   const [cart, setcart] = useState([]);

   const updateSelectedSize = (item, selectedSize) => {
   item.size = selectedSize;
   return item;
};

const handleAddtoCart = (item, selectedSize) => {
   const updatedItem = updateSelectedSize(item, selectedSize);
   let newitemArray = [...cart, updatedItem];
   setcart(newitemArray);

   console.log("STATE NOT UPDATED YET", cart);
};

 console.log("STATE UPDATED", cart);

 return (
   <div className="App">
      <i onClick={() => { handleAddtoCart({ size: 3 }, 2) }}>
       CLICK TO SET
     </i>
   </div>
 );
}

Best.最好。

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

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