Routing doesn't work on load on my react native app


username

I'm working on a React native project with a login system using Firebase. The login system works normally. I also managed to capture the current user id from Firebase. I tried setting "stay logged in" with the asyncStorage system and current user id. Even async storage works fine. But my problem is that if the async storage in is not empty, I set the redirect method to homepage componentDidMount(). The alert system I set up for testing clearly shows the correctly stored value on every reload . But redirecting to homepage doesn't work.asyncStorage

Here is my code:

            import React from 'react';
            import { StyleSheet, 
                    Text, 
                    View, 
                    TouchableOpacity,
                    AsyncStorage,
                    } from 'react-native';
            import {RkTextInput, RkButton } from 'react-native-ui-kitten';
            import {Actions} from 'react-native-router-flux';

            import * as firebase from 'firebase';

            export default class Login extends React.Component {

            constructor(props){
                super(props)

                this.state=({
                email:'[email protected]',
                password:'123123',
                userId:'',
                errorMessage: null
                })
            }

            componentDidMount() {
                this._loadInitialState().done();
            }
                _loadInitialState = async () => {
                let value= await AsyncStorage.getItem('user');
                if (value !== null){
                    this.Home
                }
                }
                signup() {
                Actions.signup()
                }
                Home() {
                Actions.home()
                }

                handleLogin = (email, password) => {

                firebase.auth().signInWithEmailAndPassword(email, password).then(
                    this.Home, 
                    this.state=({userId:firebase.auth().currentUser.uid})
                ).catch(function(error) {
                    var errorCode = error.code;
                    var errorMessage = error.message;

                    if (errorCode === 'auth/wrong-password') {
                        alert('Wrong password.');
                    } else {
                        alert(errorMessage);         
                    }
                    console.log(error);
                });
                //--------------------------Async Test---------------------
                AsyncStorage.setItem('user', this.state.userId);
                //---------------------------------------------------------
                }

            render() {
                return (
                <View style={styles.container}>

                    <Text style={styles.titleText}>Taams</Text>
                    <Text style={styles.edition}>Developer's Edition</Text>
                    <Text style={styles.titleText}>Login.js</Text>
                    <Text>Alpha 0.0.0.1</Text>

                    {/*-----UserName Input-------*/}
                    <RkTextInput 
                        rkType= 'rounded' 
                        labelStyle= {{color: 'black', fontWeight: 'bold'}}
                        placeholder='UserName'
                        //--------------value Handler----------------//
                        onChangeText={(email) => this.setState({email})}

                        //---------------------------------//
                        selectionColor="#000000"
                        keyboardType="email-address"
                        onSubmitEditing={() => { this.password.focusInput(); }}
                        inputStyle={{
                        color: 'black',
                        fontWeight: 'bold',
                        }}/>


                    {/*-----Password-------*/}
                    <RkTextInput 
                        secureTextEntry={true}
                        rkType= 'rounded' 
                        placeholder='Password'
                        //--------------value Handler----------------//
                        onChangeText={(password) => this.setState({password})}

                        //---------------------------------//
                        ref={(input) => { this.password = input; }}
                        inputStyle={{
                        color: 'black',
                        fontWeight: 'bold',
                        }}/>

                    <RkButton onPress = {()=>this.handleLogin(this.state.email,this.state.password)}>
                    <Text style={styles.LoginButtonText}>Login</Text>
                    </RkButton>

                    <View style={styles.signupTextCont}>
                        <Text style={styles.signupText}>Don't have an account yet?</Text>
                        <TouchableOpacity onPress={this.signup}><Text style={styles.signinButton}>SignUp</Text></TouchableOpacity> 
                    </View>
                </View>

                );
            }
            }

            const styles = StyleSheet.create({
            container: {
                flex: 1,
                backgroundColor: '#fff',
                alignItems: 'center',
                justifyContent: 'center',
            },
            signupTextCont:{
                flexGrow: 0,
                alignItems:'center',
                justifyContent:'flex-end',
                marginVertical:15
            },
            signupText:{
                color:'rgba(64,64,64,0.6)',
                fontSize:16
            },
            signinButton:{
                color:'#000000',
                fontSize:16,
                fontWeight:'500'
            },
            titleText: {
                fontSize: 20,
                fontWeight: 'bold',
            },
            edition: {
                fontSize: 15,
                //fontWeight: 'bold',
            },
            TextInput: {
                width: 300,
                height:50,
                borderColor: 'grey',
                borderWidth: 1,
            },
            LoginButtonText: {
                fontSize: 20,
                fontWeight: 'bold',
                color: 'white',
                //alignItems: 'center'

            },
            });

my router:

        import React, { Component } from 'react';
        import {Router, Stack, Scene} from 'react-native-router-flux';

        import SignUp  from './SignUp/SignUp';
        import Login   from './Login/Login';
        import Home    from "./Home/Home"
        import Profile from "./Profile/Profile"
        export default class Routes extends Component <{}>{
            render(){
                return(
                <Router>
                    <Stack key="root" hideNavBar={true}>
                    <Scene key="login" component={Login} title="Login" initial/>
                    <Scene key="signup" component={SignUp} title="Register" />
                    <Scene key="home" component={Home} title="Home" />
                    <Scene key="profile"component={Profile} title="profile"/> 
                    </Stack>
                </Router>
                )
            }
        }
Rivez Sharma

Change your calling function syntax

if (value !== null){
   this.Home()
 }

Related


Routing doesn't work on load on my react native app

username I'm working on a React native project with a login system using Firebase. The login system works normally. I also managed to capture the current user id from Firebase. I tried setting "stay logged in" with the asyncStorage system and current user id.

react-native-file-viewer doesn't work in my app

ED I try to use react-native-file-viewer in my app to view à pdf file. I have imported the lib like this:import FileViewer from 'react-native-file-viewer'; And I have à function: getPdf(){ const path = '../images/Charte.pdf'; FileViewer.open(pa

react-native-file-viewer doesn't work in my app

ED I try to use react-native-file-viewer in my app to view à pdf file. I have imported the lib like this:import FileViewer from 'react-native-file-viewer'; And I have à function: getPdf(){ const path = '../images/Charte.pdf'; FileViewer.open(pa

react-native-file-viewer doesn't work in my app

ED I try to use react-native-file-viewer in my app to view à pdf file. I have imported the lib like this:import FileViewer from 'react-native-file-viewer'; And I have à function: getPdf(){ const path = '../images/Charte.pdf'; FileViewer.open(pa

react-native-file-viewer doesn't work in my app

ED I try to use react-native-file-viewer in my app to view à pdf file. I have imported the lib like this:import FileViewer from 'react-native-file-viewer'; And I have à function: getPdf(){ const path = '../images/Charte.pdf'; FileViewer.open(pa

Routing doesn't work in React app with webpack

Kaklu I'm trying to create an reactapp webpackwhich also has some routes. "react-router-dom": "^5.2.0", The route works, but when I go to a specific route and reload the page, I get the error Cannot get /about import React from "react"; import { BrowserRou

Fontawesome doesn't work in my React app

Dinosaur I installed font-awesome via npm. "dependencies": { "@fortawesome/fontawesome-svg-core": "^1.2.15", "@fortawesome/free-solid-svg-icons": "^5.7.2", "@fortawesome/react-fontawesome": "^0.1.4", "axios": "^0.18.0",

EXPO React Native App doesn't load from Windows

Kitty猫 Hello everyone, I am starting a project with a team member. Our goal is to create an React Nativeapplication using Expo. This is not my first time using Expoor React Native. Question: I use Ubuntu 18.04, my friends use Windows 10. As always Windows, scr

EXPO React Native App doesn't load from Windows

Kitty猫 Hello everyone, I am starting a project with a team member. Our goal is to create an React Nativeapplication using Expo. This is not my first time using Expoor React Native. Question: I use Ubuntu 18.04, my friends use Windows 10. As always Windows, scr

EXPO React Native App doesn't load from Windows

Kitty猫 Hello everyone, I am starting a project with a team member. Our goal is to create an React Nativeapplication using Expo. This is not my first time using Expoor React Native. Question: I use Ubuntu 18.04, my friends use Windows 10. As always Windows, scr

EXPO React Native App doesn't load from Windows

Kitty猫 Hello everyone, I am starting a project with a team member. Our goal is to create an React Nativeapplication using Expo. This is not my first time using Expoor React Native. Question: I use Ubuntu 18.04, my friends use Windows 10. As always Windows, scr

Why doesn't my ESLint profile work with React Native?

Sonny Using VSCode, I created a React Native app using expo-cli. I want to use ESLint and Prettier to improve code quality. I want ESLint in the "Problems" tab to print out the error, for example if I have this line var asd = 1;in App.js then ESLint will print

this.setState doesn't work in my React native project

User 5553868 In a React Native app I'm developing, this.setState doesn't work and doesn't update the value in the constructor. But this.state({}) is working. but when i use it it is resetting the constructor anyone please point out what is wrong with this code

this.setState doesn't work in my React native project

User 5553868 In a React Native app I'm developing, this.setState doesn't work and doesn't update the value in the constructor. But this.state({}) is working. but when i use it it is resetting the constructor anyone please point out what is wrong with this code

Why doesn't my ESLint profile work with React Native?

Sonny Using VSCode, I created a React Native app using expo-cli. I want to use ESLint and Prettier to improve code quality. I want ESLint in the "Problems" tab to print out errors, for example if I have this line var asd = 1;in App.js , ESLint will print out U

Why doesn't my ESLint profile work with React Native?

Sonny Using VSCode, I created a React Native app using expo-cli. I want to use ESLint and Prettier to improve code quality. I want ESLint in the "Problems" tab to print out the error, for example if I have this line var asd = 1;in App.js then ESLint will print

Why doesn't my ESLint profile work with React Native?

Sonny Using VSCode, I created a React Native app using expo-cli. I want to use ESLint and Prettier to improve code quality. I want ESLint in the "Problems" tab to print out errors, for example if I have this line var asd = 1;in App.js , ESLint will print out U

Why doesn't my ESLint profile work with React Native?

Sonny Using VSCode, I created a React Native app using expo-cli. I want to use ESLint and Prettier to improve code quality. I want ESLint in the "Problems" tab to print out errors, for example if I have this line var asd = 1;in App.js , ESLint will print out U

In react native app, onclick function doesn't work

Sri Raman I am a new local. I wrote the following code to call a function if the user clicks on the text. var login = React.createClass({ openPopup: function(){ console.log("function called"); }, render: function(){ return (

WebStorm-React-Native App Simulator doesn't work

RC_02 I created a brand new React-Native app using WebStorm. When I click Run -> iOS triggers in iOS Simulator, I don't see iOS Simulator load. I don't see any errors either. Here is the screenshot after running the project. Now I can run react-native run-iost

In react native app, onclick function doesn't work

Sri Raman I am a new local. I wrote the following code to call a function if the user clicks on the text. var login = React.createClass({ openPopup: function(){ console.log("function called"); }, render: function(){ return (

In react native app, onclick function doesn't work

Sri Raman I am a new local. I wrote the following code to call a function if the user clicks on the text. var login = React.createClass({ openPopup: function(){ console.log("function called"); }, render: function(){ return (

In react native app, onclick function doesn't work

Sri Raman I am a new local. I wrote the following code to call a function if the user clicks on the text. var login = React.createClass({ openPopup: function(){ console.log("function called"); }, render: function(){ return (

WebStorm-React-Native App Simulator doesn't work

RC_02 I created a brand new React-Native app using WebStorm. When I click Run -> iOS triggers in iOS Simulator, I don't see iOS Simulator load. I don't see any errors either. Here is the screenshot after running the project. Now I can run react-native run-iost