javascript - 需要访问 React Native 中底部选项卡导航器中的状态以实现购物车徽章

标签 javascript react-native state badge navigator

有没有办法可以访问底部选项卡栏导航器中的 redux 状态,以便可以实现采用变量的徽章。这是要在底部选项卡栏上实现的徽章类型,特别是购物车图标,因此它可以指示购物车中的商品数量。

    Cart: {
      screen: Cart,
      navigationOptions: {
        tabBarIcon: ({ tintColor }) => (
          <IconBadge
            MainElement={
              <Image
               source={require("./assets/icons/Cart.png")}
               style={{ height: 24, width: 24, tintColor: tintColor }}
               />
            }
            BadgeElement={
              <Text style={{color:'#FFFFFF'}}>{this.state.BadgeCount}</Text>
            }
            IconBadgeStyle={
             {width:30,
              height:30,
              backgroundColor: '#FF00EE'}
            }
            Hidden={this.state.BadgeCount==0}   //how can we get access to state/props/cartItems in the bottom tab bar
          />
        )
      }
    },

badge

最佳答案

我的解决方案是创建一个名为 CartIcon 的单独组件,它将连接到 redux 存储并将其设置为 tabBarIcon。您可以创建 CartIcon 组件,如下所示:

class CartIcon extends Component {
  render() {
    return (
      <IconBadge
        MainElement={
          <Image
            source={require("./assets/icons/Cart.png")}
            style={{ height: 24, width: 24, tintColor: tintColor }}
          />
        }
        BadgeElement={
          <Text style={{ color: '#FFFFFF' }}>{this.props.cartReducer.items.length}</Text>
        }
        IconBadgeStyle={
          {
            width: 30,
            height: 30,
            backgroundColor: '#FF00EE'
          }
        }
        Hidden={this.props.cartReducer.items.length === 0}   //how can we get access to state/props/cartItems in the bottom tab bar
      />
    );
  }
}

const mapStateToProps = (state) => ({
  cart: state.cartReducer
});

export default connect(mapStateToProps, null)(CartIcon);

现在,在您的路由器 Cart 组件中作为该图标组件,如下所示:

Cart: {
  screen: Cart,
  navigationOptions: {
    tabBarIcon: ({ tintColor }) => (
      <CartIcon /> //set cart icon
    )
  }
},

关于javascript - 需要访问 React Native 中底部选项卡导航器中的状态以实现购物车徽章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59275874/

相关文章:

ios - React Native 排毒记录用户交互测试脚本

react-native - react-native-paper 样式宽度菜单

javascript - 为什么即使使用 onChange 监听器也无法更改 React 中的输入值

android - Jetpack 撰写 : Modify Room data class using TextField

javascript - 如何将 javascript Date() 转换为 COleDateTime

javascript - node.js:使用等待消耗 promise

reactjs - 如何在 React Native 中从孙子到祖 parent 进行通信,然后再返回到祖 parent 的 child

validation - 帮助我更好地理解 Struts2、验证和有状态操作

javascript - Angular 4/无法通过id从数组中检索数据

javascript - Uncaught ReferenceError : Invalid left-hand side in assignment error