【问题标题】:React Native, component is not closingReact Native,组件未关闭
【发布时间】:2020-10-16 12:38:41
【问题描述】:

我使用 react native 和 redux 创建了一个购物应用程序,我遇到了一个问题,当用户单击购买时,它应该在底部查看一个组件,该组件将显示总金额和查看购物车按钮,以及当用户从购物车中删除商品时购物车是空的,它应该关闭/状态设置为 false,但它不会消失。谁能告诉我怎么了,下面是我的代码 reducer.js

 if (action.type === SHOW_CART) {
    let addedItem = state.jeans.find((item) => item.id === action.id);
    if (addedItem == 0) {
      return {
        ...state,
        show: console.log(state.showCart),
      };
    } else {
      return {
        ...state,
        show: console.log(action.showCart),
      };
    }
  }
const initialstate = {
  showChart: false,
}

这是我处理该功能的减速器

action.js

export const showCart = (id) => {
  return {
    type: SHOW_CART,
    showCart: true,
    id,
  };
};

这是我的行为,我描述它的行为

ViewCart.js

 <View>
        {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}
      </View>
const mapStateToProps = (state) => {
  return {
    total: state.clothes.total,
    show: state.clothes.show,
  };
};

这就是我使用减速器的功能

【问题讨论】:

    标签: javascript reactjs react-native react-redux


    【解决方案1】:

    在我看来,问题出在你的 reducer.js 文件中

    if (action.type === SHOW_CART) {
        let addedItem = state.jeans.find((item) => item.id === action.id);
        if (addedItem.quantity > 0) {
          return {
            ...state,
            showCart: action.showCart, // here was the problem
          };
        }
     }
     const initialstate = {
         showChart: false,
     }
    

    请尝试一下,然后告诉我发生了什么。 让我建议在您的 if 语句中添加另一个测试: if (addItem && addedItem.quantity > 0) 为了避免意外操作可能造成的麻烦

    【讨论】:

    • nop,我做了 console.log 并更新了我的代码,我也在这里更新了它,当我在购物车中添加商品时,它使我的条件为真,但当我删除它时,它不会再次使它为假
    猜你喜欢
    • 2019-04-26
    • 2016-11-08
    • 2017-11-11
    • 2021-05-07
    • 1970-01-01
    • 2020-03-20
    • 2023-03-03
    • 2020-08-22
    • 2021-03-23
    相关资源
    最近更新 更多