KRJaguar
KRJaguar

Reputation: 15

React-Navigation (android) navigate error

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

Upvotes: 1

Views: 475

Answers (3)

KRJaguar
KRJaguar

Reputation: 15

Oops!! Solved it! Thanks you!!

index.js

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

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

Upvotes: 0

KRJaguar
KRJaguar

Reputation: 15

I see your answer and try but your code is good and work! however my code is not working... tried in many ways but i haven't solved it.

HomeScreen.js

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


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

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

Error -> Can't find variable: navigation

Upvotes: 0

Mayank Pandav
Mayank Pandav

Reputation: 455

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

Upvotes: 1

Related Questions