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

VK1 picture VK1 · Jan 11, 2018 · Viewed 13.7k times · Source

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')

Answer

Samuli Hakoniemi picture Samuli Hakoniemi · Jan 11, 2018

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.