React Native,redux 函数在更新后不会恢复到原始状态

当用户将商品添加到购物车时,我创建了一个功能来查看每个屏幕下方的购物车详细信息,当用户删除功能时,购物车详细信息将再次隐藏,但是当我从购物车中删除商品时,购物车详细信息没有隐藏,有人可以告诉我出了什么问题,下面是我的代码


减速器


 if (action.type === SHOW_CART) {

    let addedItem = state.addedItems;

    if (addedItem === 0) {

      console.log(addedItem);

      return {

        ...state,

        show: state.showCart,

      };

    } 

  } 



const initialstate = {

  showChart: false,

  addedItems: [],

}

这是我执行该功能的 redux 代码,addItems 是我的购物车,它是空白数组


行动


export const showCart = (id) => {

  return {

    type: SHOW_CART,

    showCart: true,

    id,

  };

};

这是我的行动


查看购物车


  {this.props.show ? (

          <View style={styles.total}>

            <Text style={styles.totaltext}>Total:</Text>

            <Text style={styles.priceTotal}>{this.props.total}</Text>

            <View style={styles.onPress}>

              <Text

                style={styles.pressText}

                onPress={() => RootNavigation.navigate("Cart")}

              >

                View Cart

              </Text>

            </View>

          </View>

        ) : null}

这是我的查看购物车详细信息,当用户将商品添加到购物车时我会显示购物车详细信息有人可以帮忙吗


慕尼黑5688855
浏览 149回答 2
2回答

largeQ

你应该比较长度,你目前正在直接比较数组,所以它也会去错误的路径,所以先改变它。if (action.type === SHOW_CART) {&nbsp; &nbsp; let addedItem = state.addedItems;&nbsp; &nbsp; if (addedItem.length === 0) {&nbsp; &nbsp; &nbsp; console.log(addedItem);&nbsp; &nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; ...state,&nbsp; &nbsp; &nbsp; &nbsp; show: state.showCart,&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; ...state,&nbsp; &nbsp; &nbsp; &nbsp; show: action.showCart,&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }&nbsp; }

守着一只汪

在视图购物车中,&nbsp;{this.props.show && this.props.items.length >0 ? (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <View style={styles.total}>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Text style={styles.totaltext}>Total:</Text>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Text style={styles.priceTotal}>{this.props.total}</Text>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <View style={styles.onPress}>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; style={styles.pressText}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onPress={() => RootNavigation.navigate("Cart")}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; View Cart&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Text>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </View>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </View>&nbsp; &nbsp; &nbsp; &nbsp; ) : null}const mapStateToProps = (state) => {&nbsp; return {&nbsp; &nbsp; total: state.clothes.total,&nbsp; &nbsp; show: state.clothes.show,&nbsp; &nbsp; items: state.clothes.addedItems,&nbsp; };};
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript