我创建了一个基本的SplashScreen.tsx
,上面只有一些文字。然后,我创建了这个LoginCreateAccountScreen.tsx
,它具有一个Login按钮和Create Account按钮。我希望我的应用在自动导航到SplashScreen.tsx
之前显示LoginCreateAccountScreen.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;
当我按下“登录”或“创建帐户”按钮时,出现以下错误,两者均相同:
最佳答案
这是反应导航有用的地方。这会给您带来不适。我相信您已经按照文档安装了反应导航。否则,请确保先下载该文件。选中此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")
这可以工作。希望能帮助到你。毫无疑问