不知道如何在React Native应用中使用navigation.navigate

时间:2019-12-10 01:17:42

标签: javascript ios reactjs react-native navigation

我创建了一个基本的SplashScreen.tsx,上面只有一些文字。然后,我创建了这个LoginCreateAccountScreen.tsx,它具有一个Login按钮和Create Account按钮。我希望我的应用在自动导航到SplashScreen.tsx之前显示LoginCreateAccountScreen.tsx几秒钟。同样,按按钮应该将用户重定向到其他屏幕,但这也不起作用。我不知道该怎么做,并且在弄清楚如何实现这一目标上遇到了很多困难。

我使用了此React Native Navigation ArticleThis Tutorial以及各种StackOverflow帖子,到达了现在的位置。但是,老实说,我非常迷失,因为前端导航方面正在发生很多事情。

我不了解AppNavigator.jsApp.js(应用程序的入口点)如何一起工作,以使我能够从当前屏幕导航到另一个屏幕。目前,我无法进行任何形式的导航。我必须通过在App.js中进行设置来手动更改屏幕。

我的App.js,用于渲染SplashScreen。但是不会过渡到LoginCreateAccountScreen。

import React, { Component } from 'react';

import EmojiDict from './screens/EmojiDict';
import SplashScreen from './screens/SplashScreen';
import LoginCreateAccountScreen from './screens/LoginCreateAccountScreen'

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

我的AppNavigator.js

import { createAppContainer, createSwitchNavigator } from "react-navigation";
import { createStackNavigator } from "react-navigation";
import MainTabNavigator from "./MainTabNavigator";
import { Platform } from "react-native";
// Importing my screens here.
import LoginCreateAccountScreen from "../screens/LoginCreateAccountScreen";
import CreateAccountScreen from "../screens/CreateAccountScreen";
import LoginScreen from "../screens/Login/LoginScreen";
import SplashScreen from "../screens/SplashScreen";

const MainNavigator = createStackNavigator({
  SplashScreen: {screen: SplashScreen},
  LoginCreateAccountScreen: {screen: LoginCreateAccountScreen},
  LoginScreen: {screen: LoginScreen},
  CreateAccountScreen: {screen: CreateAccountScreen},
});

const App = createAppContainer(MainNavigator);

export default App;

我的SplashScreen.tsx

import React, { Component } from 'react';
import { View, Text, StyleSheet, FlatList } from 'react-native';

class SplashScreen extends Component {
    render() {
        return (
            <View style={styles.container}>
                <Text style={styles.title}>
                    The Good App
                </Text>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
    },
    title: {
        fontSize: 45,
        textAlign: 'center',
        fontWeight: 'bold'
    }
});

export default SplashScreen;

我的LoginCreateAccountScreen.tsx

import React, { Component } from 'react';
import {
  StyleSheet,
  Button,
  View,
  SafeAreaView,
  Text,
  Alert,
  TouchableOpacity
} from 'react-native';
import Constants from 'expo-constants';

function Separator() {
  return <View style={styles.separator} />;
}

class CreateAccountOrLogin extends Component {
    handleLogInButton = () => {
        Alert.alert('Log In pressed')
        this.props.navigation.navigate("LoginScreen");
    };

    handleCreateAccountButton = () => {
        Alert.alert('Create Account pressed')
        this.props.navigation.navigate("CreateAccountScreen");
    };

    render() {
        return (
            <View style={styles.container}>
                <TouchableOpacity
                style={styles.customButtonBackground}
                onPress={() => this.handleLogInButton()}>
                    <Text style={styles.customButtonText}>Login</Text>
                </TouchableOpacity>
                <TouchableOpacity
                style={styles.customButtonBackground}
                onPress={() => this.handleCreateAccountButton()}>
                    <Text style={styles.customButtonText}>Create Account</Text>
                </TouchableOpacity>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
    },
    /* Here, style the text of your button */
    customButtonText: {
        fontSize: 35,
        fontWeight: '400',
        color: "#fff",
        textAlign: 'center'
    },
    /* Here, style the background of your button */
    customButtonBackground: {
        backgroundColor: "#007aff",
        paddingHorizontal: 30,
        paddingVertical: 5,
        borderRadius: 30,
        width: "70%",
        aspectRatio: 5 / 1,
    }
});

export default CreateAccountOrLogin;

当我按下“登录”或“创建帐户”按钮时,出现以下错误,两者均相同: enter image description here

2 个答案:

答案 0 :(得分:1)

这是反应导航有用的地方。这会给您带来不适。我相信您已经按照文档安装了反应导航。否则,请确保先下载该文件。选中此react-navigatoin-doc。希望对您有所帮助。

因此,让我们首先从堆栈导航器开始,所以您想在开始时显示启动画面,然后向右导航到登录画面。所以AppNavigator应该看起来像这样:

import { createAppContainer, createSwitchNavigator } from "react-navigation";
import { createStackNavigator } from "react-navigation";
import MainTabNavigator from "./MainTabNavigator";
import { Platform } from "react-native";
// Importing my screens here.
import LoginCreateAccountScreen from "../screens/LoginCreateAccountScreen";
import CreateAccountScreen from "../screens/CreateAccountScreen";
import LoginScreen from "../screens/Login/LoginScreen";
import SplashScreen from "../screens/SplashScreen";

const MainNavigator = createStackNavigator({
  SplashScreen: {screen: SplashScreen},
  LoginCreateAccountScreen: {screen: LoginCreateAccountScreen},
  LoginScreen: {screen: LoginScreen},
  CreateAccountScreen: {screen: CreateAccountScreen},
},
{ initialRouteName: 'SplashScreen'} // have added this coz you want splashscreen to be first rendered
);

const App = createAppContainer(MainNavigator);

export default App;

因此,既然您的应用程序导航器已经制作完毕,现在让我们切换到初始屏幕,在该屏幕上您要呈现它的时间假设为3秒钟,然后将其导航到LoginScreen。因此,我们将重定向到componentDidMount中的登录屏幕,让我们构建一个。

启动画面:

import React, { Component } from 'react';
import { View, Text, StyleSheet, FlatList } from 'react-native';

class SplashScreen extends Component {

componentDidMount(){

this.setTimeout(() => {this.props.navigation.navigate("LoginScreen")} , 3000); // im redirecting to login screen after 3 secs of showing splash screen.
}

    render() {
        return (
            <View style={styles.container}>
                <Text style={styles.title}>
                    The Good App
                </Text>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
    },
    title: {
        fontSize: 45,
        textAlign: 'center',
        fontWeight: 'bold'
    }
});

export default SplashScreen;

然后,在登录屏幕中,您可以通过this.props.navigation.navigate("ScreenName")将用户从登录重定向到任何屏幕,只需确保在执行上述代码之前先在AppNavigator中添加屏幕即可,例如

import ScreenName from "../screens/ScreenName";

    const MainNavigator = createStackNavigator({
      SplashScreen: {screen: SplashScreen},
      LoginCreateAccountScreen: {screen: LoginCreateAccountScreen},
      LoginScreen: {screen: LoginScreen},
      CreateAccountScreen: {screen: CreateAccountScreen},
     ScreenName:{screen:ScreenName}
    },
    { initialRouteName: 'SplashScreen'} // have added this coz you want splashscreen to be first rendered
    ); 

然后只有this.props.navigation.navigate("ScreenName")个可行。

希望有帮助。毫无疑问

答案 1 :(得分:0)

错误消息说,“您的“创建帐户登录”组件中没有导航道具。”

您需要将CreateAccountOrLogin添加到createStackNavigator,然后createStackNavigator将导航道具传递到CreateAccountOrLogin。

const MainNavigator = createStackNavigator({
  SplashScreen: {screen: SplashScreen},
  LoginCreateAccountScreen: {screen: LoginCreateAccountScreen},
  LoginScreen: {screen: LoginScreen},
  CreateAccountScreen: {screen: CreateAccountScreen},
+ CreateAccountOrLogin
});

只需在CreateAccountOrLogin中编写以下代码即可知道props.navigation是否存在。

render() {
  console.log(this.props) 
  return(