【问题标题】:Changing data of AsyncStorage Item REACT NATIVE更改 AsyncStorage 项 REACT NATIVE 的数据
【发布时间】:2020-04-10 10:24:23
【问题描述】:

我正在一个 Android 应用中构建一个购物车。我被困在删除购物车上的物品上。我的系统很简单。当有人在购物车上添加东西时:一个对象首先存储在 AsyncStorage 中。然后在我的购物车组件中,我也使用 AsyncStorage 获取项目。此商品称为“购物车”。

在我的购物车视图中,我有每件商品的图片、价格和数量。两个按钮允许用户修改每个项目的数量。但我在删除该项目时遇到了困难。我使用 slice 使项目消失,但它只是从我的购物车组件中消失,并且该项目仍在 AsyncStorage 上,因此每次我刷新购物车组件时,使用切片删除的项目都会使用 GetItem 函数重新出现。

所以我试图从 AsyncStorage 和我的购物车组件中删除该对象。 这是我的 AssyncStorage 商品购物车:

onClickAddCart(data){

    const itemcart = {
      food: data,
      quantity:  1,
      price: data.price
    }

    AsyncStorage.getItem('cart').then((datacart)=>{
        if (datacart !== null) {
          // We have data!!
          const cart = JSON.parse(datacart)
          cart.push(itemcart)
          AsyncStorage.setItem('cart',JSON.stringify(cart));
        }
        else{
          const cart  = []
          cart.push(itemcart)
          AsyncStorage.setItem('cart',JSON.stringify(cart));
        }
        alert("Add Cart")
      })
      .catch((err)=>{
        alert(err)
      })
  }

}

这是我得到的物品

componentDidMount(){
AsyncStorage.getItem('cart').then((cart)=>{
  if (cart !== null) {
    const cartfood = JSON.parse(cart)
    this.setState({dataCart:cartfood})
  }
})
.catch((err)=>{
  alert(err)
})

}

这是我的购物车系统

onChangeQuat(i, type){
        const cart = this.state.dataCart
        let cant = cart[i].quantity;

        if(type){
            cant = cant + 1
            cart[i].quantity = cant
            this.setState({
                dataCart:cart
            })
        }
        else if (type == false&&cant>=2){
            cant = cant -1
            cart[i].quantity = cant
            this.setState({
                dataCart:cart
            })
        }
        else if (type==false&&cant==1){
            AsyncStorage.setItem(cart, JSON.stringify({quantity: null }));
            cart.splice(i,1)
            this.setState({
                dataCart:cart
            })
        }
    }

我得到的错误是: "您尝试将键 '0' 的值设置为..."quantity":1...在一个本应是不可变且已被冻结的对象上。

【问题讨论】:

    标签: reactjs react-native react-native-android cart asyncstorage


    【解决方案1】:

    您可以结合使用 AsyncStorage.setItem() 和 AsyncStorage.mergeItem() 来使用购物车项目更新购物车。

    removeCartItem = async (index) => {
        try {
            const cart = await AsyncStorage.getItem('cart');
            let cartItems = JSON.parse(cart);
            const updatedCartItems = cartItems.filter(function (e, itemIndex) { return itemIndex !== index });
    
            await AsyncStorage.setItem('cart', JSON.stringify(updatedCartItems));
            await AsyncStorage.mergeItem('cart', JSON.stringify(updatedCartItems));
    
        } catch (error) {
            console.log('error: ', error);
        }
    };
    

    【讨论】:

    • 我不能...我尝试了这些,但它只是在购物车中更新。 AsyncStorage 仍然返回第一个
    • 你是不是刚设置好就想获取数据?
    • 我正在尝试获取数据以删除它...就像当您单击购物车中的删除按钮时,它将从购物车中删除。
    • 尝试在每个异步存储操作之前添加等待。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    相关资源
    最近更新 更多