我有一个购物车图标作为几乎所有屏幕的右上角按钮。我正在使用react-navigation5。 我想从组件中更新该按钮上的徽章计数。 没有使用redux或flux库的任何可能的方法来实现它? 以下是我的代码,例如:
route.js
import * as React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import { Home, Cart, Wishlist, Login, Signup, Contact, Profile, Search } from './scenes';
import { Primary, Secondary } from './assets/color';
import { HeaderButton } from './components'
const Stack = createStackNavigator();
function Route() {
return (
<Stack.Navigator initialRouteName="Profile" screenOptions={({ navigation, route }) =>({ headerRight:()=>(<HeaderButton navigation={navigation} nav='Cart'/>)})}>
<Stack.Screen name="Home" component={Home} />
<Stack.Screen name="Cart" component={Cart} options={{headerRight:null}}/>
<Stack.Screen name="Search" component={Search} />
</Stack.Navigator>
);
}
export default Route;
headerButton.js
import React, { Component } from "react";
import { View } from 'react-native'
import { Button, Icon, Badge, Text } from 'native-base';
import { Primary, Danger } from '../../assets/color'
export class HeaderButton extends Component{
render(){
return(
<>
<Button rounded transparent onPress={()=>this.props.navigation.push('Cart')}>
<Icon type='MaterialIcons' name='shopping-cart' style={{color:Primary,right:6,top:2}}/>
</Button>
<Text>{this.props.count}</Text>
</>
);
}
}
home.js
import React, { Component } from "react";
import { View } from 'react-native'
import { Button, Icon, Badge, Text } from 'native-base';
import { Primary, Danger } from '../../assets/color'
export class Home extends Component{
constructor(){
super();
this.state={
count:1
}
}
addToCart=()=>{
var varcount=this.state.count;
varcount=varcount+1;
this.setState({count:varcount})
}
render(){
return(
<Button onPress={this.addToCart}>
<Text>Add to cart</Text>
</Button>
);
}
}
无论何时在应用程序中更改计数,它都必须反映在route.js中的headerbutton组件中,标题为headerRight
答案 0 :(得分:1)
我自己解决了这个问题,使用了将参数从父级传递到子级,反之亦然的方法。 以下是我在代码中所做的更改。
route.js
import * as React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import { Home, Cart, Wishlist, Login, Signup, Contact, Profile, Search } from './scenes';
import { Primary, Secondary } from './assets/color';
import { HeaderButton } from './components'
const Stack = createStackNavigator();
function Route() {
const [count, setCount] = React.useState(0);
const onCount=(value)=>{
setCount(value);
console.log(count, value);
}
return (
<Stack.Navigator initialRouteName="Profile" screenOptions={({ navigation, route }) =>({ headerRight:props =>(<HeaderButton navigation={navigation} nav='Cart' {...props} badgeCount={count}/>)})}>
<Stack.Screen name="Home" component={Home} />
<Stack.Screen name="Cart" component={Cart} options={{headerRight:null}}/>
<Stack.Screen name="Search" component={Search} />
</Stack.Navigator>
);
}
export default Route;
headerButton.js
import React, { Component } from "react";
import { View } from 'react-native'
import { Button, Icon, Badge, Text } from 'native-base';
import { Primary, Danger } from '../../assets/color'
export class HeaderButton extends Component{
render(){
return(
<>
<Button rounded transparent onPress={()=>this.props.navigation.push('Cart')}>
<Icon type='MaterialIcons' name='shopping-cart' style={{color:Primary,right:6,top:2}}/>
</Button>
<Text>{this.props.badgeCount}</Text>
</>
);
}
}
home.js
import React, { Component } from "react";
import { View } from 'react-native'
import { Button, Icon, Badge, Text } from 'native-base';
import { Primary, Danger } from '../../assets/color'
export class Home extends Component{
constructor(){
super();
this.state={
count:1
}
}
addToCart=()=>{
var varcount=this.state.count;
varcount=varcount+1;
this.setState({count:varcount})
this.props.onCountChange(this.state.count);
}
render(){
return(
<Button onPress={this.addToCart}>
<Text>Add to cart</Text>
</Button>
);
}
}
答案 1 :(得分:0)
每次调用addToCart
时,都应该在其中加上括号,因为它具有函数值,应该为addToCart()
。
另外,每当您调用addToCart()
时,该值都不会更改,因为varcount
的作用域仅是本地的,我建议您将其设置为全局,这样就不会重置该值