试图操纵反应中的状态阵列
我需要编写两个功能,一个功能用于更新购物车,并使用新产品(储备金),另一种用于从购物车中删除产品。 我有很多产品供应商,所以我想将“购物车”阵列分为这样的部分:
{
"supplier1": ["product0": {props}, "product1": {props}, "productN": {props}],
"supplier2": ["product0": {props}, "product1": {props}, "productN": {props}],
"supplierN": ["product0": {props}, "product1": {props}, "productN": {props}],
}
我可以成功地将供应商添加到我的州阵列中,并将产品推向供应商的阵列。
当我使用
setCart(cart)
购物车阵列正在更新时,但这仍然是旧数组,因此React不会将我的组件恢复
,在这种情况下,
setCart([...cart])
CART数组只会变为空的
因此,这是添加新产品
const [cart, setCart] = useState([])
const addToCart = useCallback((options) => {
//options.cart_section and options.reserve
if(typeof cart[options.cart_section] === 'undefined'){
cart[options.cart_section] = []
}
cart[options.cart_section].push(options.reserve)
setCart([...cart])
}, [])
和删除产品的守则
const removeFromCart = useCallback((options) => {
let index = cart[options.cart_section].indexOf(options.reserve) - 1
cart[options.cart_section].splice(index,1)
setCart([...cart])
})
的代码,它可以删除指定的产品,但React不会恢复组件(与下面的功能相同)
对不起,我的英语,感谢您的帮助
I need to write two functions, one for updating the shopping cart with new products (reserves) and another one to remove products from the cart.
I have many product suppliers, so I want to divide my "cart" array into sections like this:
{
"supplier1": ["product0": {props}, "product1": {props}, "productN": {props}],
"supplier2": ["product0": {props}, "product1": {props}, "productN": {props}],
"supplierN": ["product0": {props}, "product1": {props}, "productN": {props}],
}
I can succesfully add suppliers to my state array and also pushing product to supplier's arrays.
When i use
setCart(cart)
cart array is updating but this is still old array, so React won't rerender my component
And in this case
setCart([...cart])
cart array just becomes empty
So here is code of adding new products
const [cart, setCart] = useState([])
const addToCart = useCallback((options) => {
//options.cart_section and options.reserve
if(typeof cart[options.cart_section] === 'undefined'){
cart[options.cart_section] = []
}
cart[options.cart_section].push(options.reserve)
setCart([...cart])
}, [])
and code of removing products form cart
const removeFromCart = useCallback((options) => {
let index = cart[options.cart_section].indexOf(options.reserve) - 1
cart[options.cart_section].splice(index,1)
setCart([...cart])
})
It removes specified product but React doesn't rerender the component (same as in function below)
Sorry for my English and thanks for the help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将对象作为数组的项目推。每个对象被认为是一个
独特的供应商。因此,您的数组应该是这样的:
之后,您可以推动新的供应商或产品,最后您应该通过setcart更新状态。这样:
这样,您正在通过SetState更新状态,React将把此操作视为(需要DOM需要更新),那么您的组件将获得Rerender
you can push objects as items of array. each object considered as a
unique supplier. So your array should be like this:
after that you can push new supplier or product and at the end you should update your state via setCart. this way:
this way you are updating your state via setState and react will treat this action as (need DOM to update) then your component will get rerender