react-native - 卡片计数功能反应原生
问题描述
我正在尝试制作一个电子商务应用程序,并且当用户将商品添加到购物车时,只有购物车图标才能工作的所有内容都不会更新。
这是我调用组件的主页:
<Header headerTitle={this.state.wineD.name} lefticonType={'back'} navigation={this.props.navigation} />
这是组件代码:
componentDidMount(){
//API code here and updating response count in state.
if(response.data.success){
this.setState({
cartItems: (response.data.data.cart.items != '' && (response.data.data.cart.items).length > 0)?
(response.data.data.cart.items).length : 0
})
this.props.changeLoaderStatus();
}
}
<FlatHeader
leftIcon={<Icon name={leftIcon} size={20} color="#FFF" />}
leftIconHandler={() => {
(this.props.lefticonType == 'bars' ?
this.props.navigation.dispatch(DrawerActions.openDrawer())
: goBack())
}}
centerContent={
<View style={{width: width*0.7,alignItems:'center'}}>
<Text numberOfLines={1} style={{ color: '#FFF',fontSize:22,fontWeight:'bold' }}>{this.props.headerTitle}</Text>
</View>
}
rightIcon={<Group><Icon name="shopping-cart" size={20} color="#FFF" />
<View style={{width:16,height:16,borderRadius:8,backgroundColor:'red',justifyContent:'center',
alignItems:'center',marginBottom:14}}>
<Text style={{fontSize:10,color:'#fff',fontWeight:'bold'}}>{this.state.cartItems}</Text></View></Group>}
rightIconHandler={() => this.props.navigation.navigate('Cart')}
large
style={{ backgroundColor: '#d7b655' }}
/>
这是从其他组件更新购物车的屏幕
任何人有解决方案,请在这里分享。
解决方案
如果我正确理解了您的问题,您可能需要以this.setState
这种方式进行修改:
componentDidMount(){
//API code here and updating response count in state.
if(response.data.success){
this.setState({
cartItems: (response.data.data.cart.items != '' && (response.data.data.cart.items).length > 0)?
(response.data.data.cart.items).length : 0
},()=>{
this.props.changeLoaderStatus(); }) } }
试试这个,让我知道它是否适合你。
推荐阅读
- c# - Webivew2 运行时和 Office/Microsoft 365
- php - WooCommerce:试图在愿望清单估算电子邮件的底部包含所有单价的总和
- ios - 在 SwiftUI 中将图像固定到视图顶部
- python - Python-docx:如何从 word 文档中删除最后一页
- python - 如何选择与雅典娜列表中所有项目匹配的行(使用python)
- c++ - Boost JSON 序列化格式(boost 1.76.0)
- asp.net - .NET Framework - 无法使用 credentialProfileStoreChain.TryGetAWSCredentials(AWSConfigs.AWSProfileName, out awsCredentials) 获取 aws 信用
- http - 编辑 HTTP POST 方法以根据 .NET Core 中的现有数据发布不同的数据
- python - Django中带有自定义字段的用户注册系统
- python - 正则表达式获取前一个单词后跟python中的一个短语