How do I pass navigation props from parent component to header?

自作多情 提交于 2020-04-10 06:22:31

问题


I have 4 pages: App.js, HomeScreen.js, Login.js, Toolbar.js

My App page with the StackNavigator:

import React, { Component } from 'react';
import {
  Platform,
  StyleSheet,
  Text,
  View
} from 'react-native';

import { StackNavigator, DrawerNavigator } from 'react-navigation';

import Splash from './screens/Splash';
import HomeScreen from './screens/HomeScreen';
import Login from './screens/Login';
import Lobby from './screens/Lobby';
import Wifi from './screens/Wifi';
import Toolbar from './components/ToolBar/Toolbar';
import Mobile from './screens/Mobile';


;

const Navigation = StackNavigator({
  Splash:{screen: Splash},
  HomeScreen:{screen: HomeScreen},
  Login:{screen: Login},
  Lobby:{screen: Lobby},
  Wifi:{screen: Wifi},
  Mobile:{screen:Mobile}
}, {
  mode: 'modal',
  headerMode: 'none'
});


export default Navigation;

I'm importing Toolbar to HomeScreen and would like to pass the navigation props from HomeScreen to Toolbar so that I can access the Login page from both the Toolbar and HomeScreen.

My Toolbar page:

import React,{Component} from 'react';
import {Text, View, Button, StyleSheet, TouchableOpacity, 
    Dimensions,
    Image,ScrollView, Picker, FlatList} from 'react-native';
import { StackNavigator, DrawerNavigator } from 'react-navigation';
import config from '../../components/config/index';




class Toolbar extends Component{
    constructor(){
        super()
        this.state = {

        }
    }
    render(){
    //const {navigate} = this.props.navigation;
        return(
            <View style={styles.mainToolbar}>
                <View style={styles.containerForThree}>
                    <View style={styles.leftArrowContainer}>

                        <TouchableOpacity
                            onPress={()=>navigate('Login')}
                        >
                            <Text>hello</Text>

                        </TouchableOpacity>

                    </View>

As of now, I have const navigate commented out because HomeScreen won't load if it's appears.

HomeScreen:

import React,{Component} from 'react';
import {
    AppRegistry, StyleSheet, Text, View, Image, ScrollView, Button, TouchableOpacity, Dimensions
    } from 'react-native';
import { StackNavigator } from 'react-navigation';
import Toolbar from '../components/ToolBar/Toolbar';

export default class HomeScreen extends Component {
    static navigationOptions ={
        title: 'Home Screen'
    };


    onValueChange(value, idx){
        this.setState({
            language: value
        })

        AsyncStorage.setItem('language', value)   
    }

    render(){
        var {navigate} = this.props.navigation;
        return(
            <View style={styles.scrollContainer}>
            <Toolbar navigate={navigate}/>
                <View>
                    <Text>Welcome To the SEcond Page</Text>
                        <Button
                            onPress={
                                ()=>navigate("Login")
                            }
                            title="Go to Login"
                        />
                </View>

I've attempted to pass navigation props from HomeScreen to Toolbar via

When I press the button, it says can't find variable 'navigate'. However, when I uncomment out navigate on Toolbar, I get the error TypeError: undefined is not an object(evaulating 'this.props.navigation.navigate')


回答1:


Here you're passing a prop navigate to Toolbar: <Toolbar navigate={navigate}/>

Therefore you need to refer it as such in render(): const { navigate } = this.props;

So only a minor mistake, but that's what fixes it.

I'd personally encourage to pass navigation to Toolbar: <Toolbar navigation={ this.props.navigation } />. Thus you get more visibility to the navigation object.




回答2:


You can use withNavigation prop of React navigation. If you want to navigate from a component. https://reactnavigation.org/docs/en/connecting-navigation-prop.html




回答3:


It's about wrapping up the withNavigation HOC to corresponding component.It's not like sending a prop to child



来源:https://stackoverflow.com/questions/48203708/how-do-i-pass-navigation-props-from-parent-component-to-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!