【发布时间】:2019-12-10 01:17:42
【问题描述】:
我创建了一个基本的SplashScreen.tsx,上面只有一些文字。然后,我创建了这个LoginCreateAccountScreen.tsx,它有一个登录按钮和创建帐户按钮。我希望我的应用程序在自动导航到 LoginCreateAccountScreen.tsx 之前显示 SplashScreen.tsx 几秒钟。此外,按下按钮应该将用户重定向到其他屏幕,但这也不起作用。我不知道如何做到这一点,并且在弄清楚如何做到这一点时遇到了很多困难。
我使用 React Native Navigation Article 和 This Tutorial 以及各种 StackOverflow 帖子到达我现在所在的位置。但老实说,我很迷茫,因为关于前端导航的事情太多了。
我不明白AppNavigator.js 和App.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;
【问题讨论】:
标签: javascript ios reactjs react-native navigation