如何在本机反应中使用长度属性?

克涅瓦吉

我目前正在学习构建一个购物应用程序并研究 React Native 应用程序的购物车功能。我制作了一个单独的购物车图标组件,我将其传递到主应用程序中。但是,当我尝试使用长度属性显示购物车图标中的商品数量时,长度并未显示。我是初学者,任何帮助将不胜感激。

购物车图标.js

import React, { Component } from 'react';
import { View, Text, Picker,Button } from 'react-native';
import { connect } from 'react-redux';
import Icon from 'react-native-vector-icons/FontAwesome';
import { Actions } from 'react-native-router-flux';

goToNextScreen = () => {
  Actions.cartScreen();
}

const ShoppingCartIcon = (props) => (

      <View style={{position:'absolute', top:0,left:0,width: 40,height: 40,borderRadius: 40/2,paddingTop:3}}>
           <View style={{
          position: 'absolute', height: 20, width: 20, borderRadius: 10, backgroundColor: 'rgba(6,21,42,0.8)', top:0, left: 5, alignItems: 'center', justifyContent: 'center', zIndex: 2000

                  }}>
                  <Text style={{ color: 'white', fontWeight: 'bold',fontSize:10 }}>{props.cartItems.length}</Text>
            </View>
          <Icon onPress={() => this.goToNextScreen()} name="shopping-cart"  style ={{fontSize:30, position:'absolute', top:0,padding:7, left:0, color:'#fe003a'}} />
      </View>
)

const mapStateToProps = (state) => {
    return {
        cartItems: state
    }
}
export default connect(mapStateToProps)(ShoppingCartIcon);

输出:

购物车图标的图像

鲨鱼中风

mapStateToProps函数中,您设置了cartItems: state,但也许您必须这样做cartItems: state.YourReducerName.yourArrayName您也可以发布reducer代码吗?

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章