屏幕之间的本机导航

时间:2019-02-24 10:01:35

标签: javascript reactjs react-native

我是react-native的初学者,我打算在我的应用程序中实现react堆栈导航。我试图使App.js呈现Home.js作为默认屏幕。我希望将app.js用作主屏幕,并添加另一个“登录”屏幕,但我做不到。我无法找出问题所在,我只是让开发服务器返回了响应错误代码:500。这是App.js的代码。

import React from 'react';
import { StyleSheet, Text, View, Button, KeyboardAvoidingView } from 'react-native';
import {createStackNavigator, createAppContainer} from 'react-navigation'

import Home from './Screens/Home';

const AppNavigator = createStackNavigator({
  Home: {screen: Home},
});

export default class App extends React.Component {

  render() {
    return (
      <AppNavigator />
    );
  }
}

这是我的Home.js文件。

import React from 'react';
import { StyleSheet, Text, View, Button, KeyboardAvoidingView } from 'react-native';
import {StackNavigator, createStackNavigator, createAppContainer} from 'react-navigation'


export default class Home extends React.Component {
  static navigationOptions = {
    title: 'Home',
  }

  render() {
    const { navigate } = this.props.navigation;
    return (
      <View>
        <Text>This is the Home HomeScreen</Text>
      </View>
    );
  }
}

2 个答案:

答案 0 :(得分:1)

您忘记了创建应用容器,请查看此链接https://reactnavigation.org/docs/en/hello-react-navigation.html

import React from 'react';
import { StyleSheet, Text, View, Button, KeyboardAvoidingView } from 'react-native';
import {createStackNavigator, createAppContainer} from 'react-navigation'

import Home from './Screens/Home';

const AppNavigator = createStackNavigator({
  Home: {screen: Home},
});

const AppContainer = createAppContainer(AppNavigator);

export default class App extends React.Component {

  render() {
    return (
      <AppContainer />
    );
  }
}

答案 1 :(得分:0)

我认为,您已经错过了“ createAppContainer”。请尝试如下代码。

const AppStackNavigator = createStackNavigator({
  Welcome: {
   screen: WelcomeScreen,
     }
 });

const AppNavigator = createAppContainer(AppStackNavigator);

然后在渲染器中使用AppNavigator。