React native 如何在登录/注册后跳转其他页面
我使用react navigation v3构建导航,并使用firebase进行身份验证。没问题。导航流程正在运行,我可以注册。我面临的问题是,当我按下注册按钮时,它不会跳转到注册屏幕 所以构建的结构是:在App.js中,我正在做导航部分。首先发送欢迎屏幕,包括登录 这是欢迎屏幕:React native 如何在登录/注册后跳转其他页面,react-native,firebase-authentication,react-navigation,react-navigation-stack,React Native,Firebase Authentication,React Navigation,React Navigation Stack,我使用react navigation v3构建导航,并使用firebase进行身份验证。没问题。导航流程正在运行,我可以注册。我面临的问题是,当我按下注册按钮时,它不会跳转到注册屏幕 所以构建的结构是:在App.js中,我正在做导航部分。首先发送欢迎屏幕,包括登录 这是欢迎屏幕: import React, { Component } from 'react' import {StyleSheet, View } from "react-native"; import {Container,
import React, { Component } from 'react'
import {StyleSheet, View } from "react-native";
import {Container, Text, Form, Content, Header, Button, Input, Label, Item} from 'native-base';
import SignUp from '../screens/register/SignUp'
import * as firebase from 'firebase';
const firebaseConfig = {
apiKey: "example example",
authDomain: "example example",
databaseURL: "example example",
projectId: "example example",
storageBucket: "example example",
};
firebase.initializeApp(firebaseConfig);
export default class WelcomeScreen extends Component {
constructor(props){
super(props)
this.state = ({
email: '',
password: ''
})
}
loginUser = (email, password, navigate) => {
try {
firebase.auth().signInWithEmailAndPassword(email,password).then(function(user){
console.log(user);
navigate('Learn')
})
}
catch (error) {
console.log(error.toString())
}
};
render() {
return (
<Container style={styles.container}>
<Form>
<Item floatingLabel>
<Label>E-mail</Label>
<Input
autocorrect={false}
autoCapitalize={'none'}
onChangeText={(email) => this.setState({email})}
/>
</Item>
<Item floatingLabel>
<Label>Password</Label>
<Input
secureTextEntry={true}
autocorrect={false}
autoCapitalize={'none'}
onChangeText={(password)=>this.setState({password})}
/>
</Item>
</Form>
<Button style={{backgroundColor:'#6c5ce7', marginTop: 10}}
onPress={()=>this.loginUser(this.state.email,this.state.password)}
rounded
success
>
<Text>Kelimeda'ya Uç</Text>
</Button>
<Button style={{backgroundColor:'#6c5ce7', marginTop: 10}}
onPress={()=>this.props.navigation.navigate('SignUp')}
rounded
primary
>
<Text>Beni Kaydet!</Text>
</Button>
</Container>
);
}
}
import React,{Component}来自“React”
从“react native”导入{StyleSheet,View};
从“原生基”导入{容器、文本、表单、内容、标题、按钮、输入、标签、项};
从“../screens/register/SignUp”导入注册
从“firebase”导入*作为firebase;
常量firebaseConfig={
apiKey:“示例”,
authDomain:“示例”,
databaseURL:“示例”,
projectId:“示例”,
storageBucket:“示例”,
};
firebase.initializeApp(firebaseConfig);
导出默认类WelcomeScreen扩展组件{
建造师(道具){
超级(道具)
此.state=({
电子邮件:“”,
密码:“”
})
}
登录用户=(电子邮件、密码、导航)=>{
试一试{
firebase.auth(){
console.log(用户);
导航(“学习”)
})
}
捕获(错误){
console.log(error.toString())
}
};
render(){
返回(
电子邮件
this.setState({email})}
/>
密码
this.setState({password})}
/>
this.login用户(this.state.email,this.state.password)}
圆的
成功
>
科利梅达乌萨
this.props.navigation.navigate('SignUp')}
圆的
主要的,重要的
>
贝尼·凯德!
);
}
}
注册屏幕:
import React, { Component } from 'react'
import {StyleSheet, View } from "react-native";
import {Container, Text, Form, Content, Header, Button, Input, Label, Item} from 'native-base';
import * as firebase from 'firebase';
export default class WelcomeScreen extends Component {
constructor(props){
super(props)
this.state = ({
email: '',
password: ''
})
}
signUpUser = (email, password) => {
try {
if(this.state.password.length < 6){
alert('Lutfen 6 dan daha uzun bir karakter giriniz.')
return
}
firebase.auth().createUserWithEmailAndPassword(email,password)
}
catch (error) {
console.log(error.toString())
}
};
render() {
return (
<Container style={styles.container}>
<Form>
<Item floatingLabel>
<Label>E-mail</Label>
<Input
autocorrect={false}
autoCapitalize={'none'}
onChangeText={(email) => this.setState({email})}
/>
</Item>
<Item floatingLabel>
<Label>Password</Label>
<Input
secureTextEntry={true}
autocorrect={false}
autoCapitalize={'none'}
onChangeText={(password)=>this.setState({password})}
/>
</Item>
</Form>
<Button style={{backgroundColor:'#6c5ce7', marginTop: 10}}
onPress={()=>this.signUpUser(this.state.email,this.state.password)}
rounded
primary
>
<Text>Beni Kaydet!</Text>
</Button>
</Container>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 5,
justifyContent: 'center',
backgroundColor: '#fff',
},
});
import React,{Component}来自“React”
从“react native”导入{StyleSheet,View};
从“原生基”导入{容器、文本、表单、内容、标题、按钮、输入、标签、项};
从“firebase”导入*作为firebase;
导出默认类WelcomeScreen扩展组件{
建造师(道具){
超级(道具)
此.state=({
电子邮件:“”,
密码:“”
})
}
注册用户=(电子邮件、密码)=>{
试一试{
if(this.state.password.length<6){
警报(‘路德芬6号和达哈·乌赞·比尔·卡拉克特·吉日化’)
返回
}
firebase.auth().createUserWithEmailAndPassword(电子邮件,密码)
}
捕获(错误){
console.log(error.toString())
}
};
render(){
返回(
电子邮件
this.setState({email})}
/>
密码
this.setState({password})}
/>
this.signUpUser(this.state.email,this.state.password)}
圆的
主要的,重要的
>
贝尼·凯德!
);
}
}
const styles=StyleSheet.create({
容器:{
弹性:1,
填充:5,
为内容辩护:“中心”,
背景颜色:“#fff”,
},
});
这是应用程序屏幕。我是否需要在此处检查用户是否已登录?还是欢迎屏幕
//imports...
import React, { Component } from 'react';
import {View, StatusBar} from 'react-native';
import {
createSwitchNavigator,
createAppContainer,
createDrawerNavigator,
createBottomTabNavigator,
createStackNavigator,} from 'react-navigation';
import Icon from 'react-native-vector-icons/Ionicons';
import WelcomeScreen from './src/screens/Welcome';
import Learn from './src/screens/Tab/Learn';
import Settings from './src/screens/Tab/Settings';
import Play from './src/screens/Tab/Play';
//content: const & functions and styles...
const DashboardTabNavigator = createBottomTabNavigator({
Play,
Learn,
Settings
},
{
navigationOptions: ({navigation}) => {
const {routeName} = navigation.state.routes
[navigation.state.index];
return {
headerTitle: routeName,
headerTintColor:'#fff',
headerStyle:{
backgroundColor: '#2c3e50',
}
};
}
});
const DashStack = createStackNavigator({
DashboardTabNavigator: DashboardTabNavigator
}, {
defaultNavigationOptions:({navigation}) =>{
return {
headerLeft: <Icon
style={{paddingLeft: 15, color:'#fff'}}
onPress={()=>navigation.openDrawer()}
name={'md-menu'}
size={30}
/>
}
},
});
const appDrawNavigator = createDrawerNavigator({
Dashboard:{ screen: DashStack }
});
const appSwitchNavigation = createSwitchNavigator({
Welcome:{ screen: WelcomeScreen },
Dashboard:{ screen: appDrawNavigator }
});
const AppContainer = createAppContainer(appSwitchNavigation);
class App extends Component {
render() {
return(
<View style={{flex: 1}}>
<StatusBar
backgroundColor="blue"
barStyle="light-content"
/>
<AppContainer/>
</View>
) ;
}
}
export default App;
//导入。。。
从“React”导入React,{Component};
从“react native”导入{View,StatusBar};
进口{
createSwitchNavigator,
createAppContainer,
createDrawerNavigator,
CreateBoottomTabNavigator,
createStackNavigator,}来自“反应导航”;
从“反应本机矢量图标/离子图标”导入图标;
从“/src/screens/Welcome”导入WelcomeScreen;
从“/src/screens/Tab/Learn”导入学习;
从“/src/screens/Tab/Settings”导入设置;
从“/src/screens/Tab/Play”导入播放;
//内容:常量、函数和样式。。。
const DashboardTabNavigator=createBottomTabNavigator({
玩
学习,
设置
},
{
导航选项:({navigation})=>{
const{routeName}=navigation.state.routes
[导航.状态.索引];
返回{
标题:routeName,
标题颜色:“#fff”,
头型:{
背景颜色:“#2c3e50”,
}
};
}
});
const DashStack=createStackNavigator({
仪表板选项卡导航器:仪表板选项卡导航器
}, {
defaultNavigationOptions:({navigation})=>{
返回{
headerLeft:navigation.openDrawer()}
名称={'md-menu'}
大小={30}
/>
}
},
});
常量appDrawNavigator=createDrawerNavigator({
仪表板:{屏幕:DashStack}
});
const appSwitchNavigation=createSwitchNavigator({
欢迎:{screen:WelcomeScreen},
仪表板:{屏幕:appDrawNavigator}
});
const AppContainer=createAppContainer(appSwitchNavigation);
类应用程序扩展组件{
render(){
返回(
) ;
}
}
导出默认应用程序;
问题1的解决方案:您尚未通过导航到登录用户
功能,因此该功能不起作用。请像这样将导航参数发送到登录用户
<Button
style={{backgroundColor:'#6c5ce7', marginTop: 10}}
onPress={()=>this.loginUser(this.state.email,this.state.password, this.props.navigation.navigate)}
rounded
success>
this.login用户(this.state.email、this.state.password、this.props.navigation.navigate)}
圆的
成功>
问题2的解决方案:用于firebase dupl
loginUser = (email, password, navigate) => {
try {
firebase.auth().signInWithEmailAndPassword(email,password).then(function(user){
console.log(user);
navigate('Learn')
})
}
catch (error) {
console.log(error.toString())
}
};
<Button
style={{backgroundColor:'#6c5ce7', marginTop: 10}}
onPress={()=>this.loginUser(this.state.email,this.state.password,this.props.navigation.navigate)}
rounded
success
>