无法在react native中读取undefined的属性'navigate'。这在app js中正常运行,但在about js中不正常

问题描述 投票:0回答:1

代码在App.js中正常运行,但在About.js中不正常

我尝试了在google中找到的所有内容,但始终给我“无法读取未定义的属性'navigate'。

我该如何解决?请帮我解决这个问题。您的帮助将不胜感激,如果需要更多信息,请让我知道

App.js

const AppNavigator = createStackNavigator({
  Home: {
    screen: HomeScreen,
  },
  Details: {
    screen: DetailsScreen,
  },
  Audios: {
    screen: FouraudioScreen,
  },
  About: {
    screen: AboutScreen,
    navigationOptions : {
        header: null
    }
  },
   Gallery: {
    screen: GalleryScreen,
    navigationOptions : {
        header: null
    }
  },
}, {
    initialRouteName: 'Home',
});

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    color:'#1c2747',
   // alignItems: 'center',
    //justifyContent: 'center',
  },
});

const AppContainer = createAppContainer(AppNavigator);

export default class App extends React.Component {
  render() {
    return <AppContainer />;
  }  
}

// About.js

render() {
   return (

        <View> 
            <Button
            title="Go to Gallery"
            onPress={() => this.props.navigation.navigate('Gallery')}
          />
        </View>
   )
}

const RootStack = createStackNavigator(
  {
    About: {
      screen: AboutScreen,
    },
    Gallery: {
      screen: GalleryScreen,
    },
  },
   {
  headerMode: 'none', 

  }
);

const AboutContainer = createAppContainer(RootStack);

class About extends React.Component {
  render() {
    return <AboutScreen />;
  }
};

export default About;
react-native react-native-android react-navigation react-native-navigation
1个回答
0
投票

添加import { withNavigation } from 'react-navigation';到About.js并将该类导出为export default withNavigation(About.js)

© www.soinside.com 2019 - 2024. All rights reserved.