React-Navigation (android) 导航错误

React-Navigation (android) navigate error

我的英语水平很低,因为我来自韩国。 那么,我需要你们的帮助…… 我正在学习 React-native (android) 我想切换我的屏幕...但反应导航不起作用。 我找到这个解决方案 1 天......但我找不到解决方案...... 请帮帮我..

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

你可以使用 react-navigation 的 withNavigaion HOC 使用下面的代码希望它能工作

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

我看到了你的答案并尝试了,但你的代码很好并且可以工作!但是我的代码不起作用...尝试了很多方法但我还没有解决它。

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

错误 -> 找不到变量:导航

糟糕!!解决了!谢谢你!!

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