简体   繁体   English

React Native-如何在页面之间导航-错误:未定义不是函数(在附近,'…(0,_reactNAvigation.StackNavigator)...')

[英]React Native - how to navigate between pages - Error: undefined is not a function(near, '…(0, _reactNAvigation.StackNavigator)…'))

I am learning React Native and I am trying to implement page navigation, when I click the Explore button , I want to go to the About page. 我正在学习React Native,并尝试实现页面导航,当我单击探索按钮时,我想转到关于页面。 I followed a few guides but nothing so far. 我遵循了一些指南,但到目前为止没有任何帮助。 The error I get is undefined is not a function (near '...(0, reactNavigation.StackNavigator)...')* . 我得到的错误是不确定的不是函数('...(0,reactNavigation.StackNavigator)...')*附近

Here is the code: 这是代码:

index.js index.js

/** @format */

import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';

AppRegistry.registerComponent(appName, () => App);

App.js App.js

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 */

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, Button, TouchableHighlight} from 'react-native';
import { createStackNavigator, StackNavigator } from "react-navigation";
import HomeScreen from './pages/home_screen.js';
import AboutPage from './pages/about_me.js';



// const instructions = Platform.select({
//   ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
//   android:
//     'Double tap R on your keyboard to reload,\n' +
//     'Shake or press menu button for dev menu',
// });



const RootStack = createStackNavigator(
  {
    Home: {
      screen: HomeScreen,
    },
    About: {
      screen: AboutPage,
    },
  },
  {
    initialRouteName: 'Home',
  }
);

const AppContainer = createAppContainer(RootStack);

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

home_screen.js home_screen.js

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 */

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, Button, TouchableHighlight} from 'react-native';
import LinearGradient  from 'react-native-linear-gradient';
import LocationIcon from 'react-native-vector-icons/Entypo';
import SocMediaIcons from 'react-native-vector-icons/AntDesign';
import { createStackNavigator, createAppContainer } from "react-navigation";




const locationIcon = (<LocationIcon name="location" size={30} color="#6600cc" />);
const linkedInIcon = (<SocMediaIcons name="linkedin-square" size={30} color="#6600cc" />);
const instagramIcon = (<SocMediaIcons name="instagram" size={30} color="#6600cc" />);
const facebookIcon = (<SocMediaIcons name="facebook-square" size={30} color="#6600cc" />);



// const instructions = Platform.select({
//   ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
//   android:
//     'Double tap R on your keyboard to reload,\n' +
//     'Shake or press menu button for dev menu',
// });

class HomeScreen extends Component{
  render() {
    return (
      <LinearGradient
          colors={[ '#276b7a', '#0072a9', '#0071d9', '#3860f5', '#bc12eb']}
          style={styles.container}
        >
       <View style={styles.iconGrid}>
        <View style={{width: 195}}>
         <Text>{locationIcon} Mordor</Text>

        </View>
        <View style={{width: 70}} />
        <View style={{width: 30}} >
          {facebookIcon}
        </View>
        <View style={{width: 30 }} >
          {instagramIcon}
        </View>
        <View style={{width: 30}} >
          {linkedInIcon}
        </View>
      </View>

        <TouchableHighlight 
                style ={{
                    height: 50,
                    shadowColor: 'red',
                    width:260,
                    borderRadius:30,
                    backgroundColor : "rgba(255, 255, 255, 0.3)",
                    marginLeft :50,
                    marginRight:50,
                    marginTop : 250
                }}>
            <Button onPress={()=> this.props.navigation.navigate('About')}            
            title="Explore"
            accessibilityLabel="Explore Beautox"
            /> 
        </TouchableHighlight> 


      </LinearGradient>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',

  },
  button : {
     borderColor: 'red',
     backgroundColor: 'rgba(255, 255, 255, 1.0)'
  },
  iconGrid: {
    flexDirection: 'row',
    marginTop: 350,
    width: 350,
    marginRight: 10
  }
});

export default HomeScreen;

about_me.js about_me.js

import React, {Component} from 'react';
import LinearGradient  from 'react-native-linear-gradient';

    class AboutMe extends Component {
        render() {
         return(
           <View>
              <Text>Hello</Text>
              <Button
               title="Go back"
               onPress={() => this.props.navigation.goBack()}
                />
              </View>
            );
        }
    };

    export default AboutMe;

Any help would be appreciated. 任何帮助,将不胜感激。

Here's a minimalist 2 page v3 app. 这是2页的极简主义v3应用程序。 Check it out as an Expo Snack for all the code. 签出它作为所有代码的世博小吃

class Home extends React.Component {
  static navigationOptions = {
    title: "Home",
  }
  render() {
    return (
      <View style={styles.container}>
        <Text>Home Page</Text>
        <Button onPress={() => this.props.navigation.navigate('About')} title="All about me" />
      </View>
    );
  }
}

class AboutMe extends React.Component {
  static navigationOptions = {
    title: "All Me",
  }
  render() {
    return (
      <View style={styles.container}>
        <Text>Home Page</Text>
        <Button onPress={() => this.props.navigation.goBack()} title="<< Back" />
      </View>
    );
  }
}

const AppScreens = createStackNavigator({
  Home: Home,
  About: AboutMe,
})

const App = createAppContainer(AppScreens);

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
});

export default App;

I'm considering you're using react-navigation v3. 我正在考虑您使用的是react-navigation v3。 And in the documentation , it is clearly mentioned that 并且在文档中 ,明确提到

Note: In v2 and earlier, the containers in React Navigation are automatically provided by the create*Navigator functions. 注意:在v2和更低版本中,React Navigation中的容器由create * Navigator函数自动提供。 As of v3, you are required to use the container directly. 从v3开始,您需要直接使用容器。 In v3 we also renamed createNavigationContainer to createAppContainer. 在v3中,我们还将createNavigationContainer重命名为createAppContainer。

So all you have to do is use the appContainer. 因此,您要做的就是使用appContainer。

Example: 例:

import { createAppContainer } from 'react-navigation';


const AppNavigator = createStackNavigator(...);

const AppContainer = createAppContainer(AppNavigator);

export default AppContainer;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native:React Navigation StackNavigator 不工作。 得到错误:“未定义不是一个对象(评估&#39;this.props.navigation.navigate&#39;)” - React Native: React Navigation StackNavigator not working. Getting error: “undefined is not an object (evaluating 'this.props.navigation.navigate')” React Native Navigation(StackNavigator):我收到一条错误消息:“ undefined不是对象(正在评估&#39;this.props.navigate&#39;)” - React Native Navigation(StackNavigator): I get an error saying “ undefined is not an object (evaluating 'this.props.navigate') ” React Native-未定义不是StackNavigator的功能问题 - React Native - Undefined is not a function issue with StackNavigator React Native:React导航StackNavigator不起作用。 出现错误:“未定义不是对象(正在评估&#39;_this3.props.navigation.navigate&#39;)” - React Native: React Navigation StackNavigator not working. Getting error: “undefined is not an object (evaluating '_this3.props.navigation.navigate')” React Native 从 StackNavigator 导航到另一个 StackNavigator 或 TabNavigator - React Native navigate from StackNavigator to another StackNavigator or TabNavigator 如何在React Native上将回调函数传递给嵌套的StackNavigator - How to pass callback function to nested StackNavigator on React Native 如何在 React JS 中的两个页面之间导航 - How to navigate between two pages in React JS React-native 搜索栏 - 错误:未定义不是 function(靠近'... this.state.books.filter...') - React-native search bar - error: undefined is not a function (near '… this.state.books.filter…') 如何在React本机组件之间导航? - How to navigate between React-native Component? 如何在 React Native 上的屏幕之间导航? - How to navigate between screen on React Native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM