简体   繁体   中英

how to pass data to navigation header in react native?

I have a cart icon as a header-right button for almost all screens. I am using react-navigation5. I want to update the badge count on that button from my components. Any possible way to achieve it without using redux or flux library? following is my code for example:

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>

        );
    }
}

Whenever count is changed in the app it must be reflected in headerbutton component which is in route.js as headerRight

When ever you call the addToCart , you should add parenthesis to it since it has a function value, it should be addToCart() . Also whenevery you call addToCart() The value will not change because the scope the varcount is only local, I suggest you make it global so it will not reset the value

I have solved it by myself, using passing parameters from parent to child and vise versa. The following are the changes I have made in my code.

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>

        );
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM