Warm tip: This article is reproduced from stackoverflow.com, please click
react-native navigation

React-Navigation (android) navigate error

发布于 2020-03-29 12:45:56

My English level is very low because i'm from Korean. then, i need you help guys.. i'm studying React-native (android) i want switch my screen... but react navigation does not working. i find this solution for 1 days... but i cant find solution... please help me guys..

HomePage.js

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

class HomeScreen extends React.Component {    
render() {             
  return (          
      <View style={styles.MasterContainer}>
          <NavBar/>
          <UserBar/>
          <View style={{height: 40,}}></View>
          <ButtonTab/>
          <Admob/>
          <TapBar/>
      </View>
  );
}
}

class NavBar extends React.Component {
render(){  
    const {navigation} = this.props;
    return(
        <View style={styles.NavBar}>
            <TouchableOpacity>
                <Text style={{fontWeight: 'bold',fontSize: 18, color: 'white'}} onPress ={() => 
navigation.navigate('NavPg')}>더보기</Text>
            </TouchableOpacity>
        </View>
    )
 }
}

App.js

import React from 'react';
import HomePage from './screens/HomeScreen';
import NavPage from './screens'
import {createAppContainer} from 'react-navigation';
import {createStackNavigator} from 'react-navigation-stack';

const App = createStackNavigator(
{
    Home: {screen:HomePage},
    NavPg: {screen:NavPage},
},
{initialRouteName:"Home", headerMode:'none'}
);

export default createAppContainer(App);
Questioner
KRJaguar
Viewed
16
Mayank Pandav 2020-01-31 17:16

you can use HOC of withNavigaion from react-navigation use below code hope it will work

 import { withNavigation } from 'react-navigation';

class NavBar extends React.Component {
render(){  
    const {navigation} = this.props;
    return(
        <View style={styles.NavBar}>
            <TouchableOpacity>
                <Text style={{fontWeight: 'bold',fontSize: 18, color: 'white'}} onPress ={() => 
navigation.navigate('NavPg')}>더보기</Text>
            </TouchableOpacity>
        </View>
    )
 }
}
export default withNavigation(NavBar);