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

3

1

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

Autochthon answered 11/1, 2018 at 9:33 Comment(0)
D
8

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.

Daughterinlaw answered 11/1, 2018 at 9:37 Comment(1)
render(): const { navigation } = this.props; --> navigation.navigate('screen')Emmen
G
3

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

Gast answered 10/8, 2018 at 7:45 Comment(0)
T
1

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

Truncate answered 28/1, 2019 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.