Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Set initial state on react-native-router-flex

import React, { Component } from 'react';
import { Router, Scene } from 'react-native-router-flux';
import Scene1 from '../Scene1';
import Scene2 from '../Scene2';

// localization strings
import strings from '../config/localization';

import styles from './Styles';

class Routes extends Component {
  state = {
    isUserLogin: false
  }
  async componentDidMount() {
    await AsyncStorage.getItem('user', (err, result) => {
      if (result != null) {
        this.setState({ isUserLogin: JSON.parse(result).isUserLoggedIn });
      }
      if (__DEV__) {
        console.log('routes', this.state); // return trur or false if user logged in or not
      }
    });
  }

// HELPER FUNCTION FOR AUTH
  authenticate = () => {
    this.state.isUserLogin ? true : false
  }

  render() {
    return {
      <Router
        backAndroidHandler={() => Actions.pop()}
        sceneStyle={styles.sceneStyle}
        >
          <Scene key="root">
            <Scene 
              key="Launch" 
              component="Launch"
              initial
              on={this.authenticate}
              success="Scene2"
              failure="Scene1"
            />
            <Scene
              key="merchants"
              component={Scene1}
              title={strings.selectBusiness}
              navigationBarStyle={styles.navigationBarStyle}
              navBarButtonColor={styles.navBarButtonColor}
              titleStyle={styles.titleStyle}
              initial={!this.state.isUserLogin}
            />
            <Scene
              key="initializeStore"
              component={Scene2}
              hideNavBar
              initial={this.state.isUserLogin}
            />
            <Scene
              key="login"
              component={Login}
              navigationBarStyle={styles.navigationBarStyle}
              navBarButtonColor={styles.navBarButtonColor}
              titleStyle={styles.titleStyle}
              back
              renderBackButton={renderBackButton}
            />
          </Scene>
      </Router>
    }
  }
}

export default Routes;
Comment

PREVIOUS NEXT
Code Example
Javascript :: get nested value on object react using dot 
Javascript :: enquirer confirm 
Javascript :: how to turn off auto refresh a href in javascript 
Javascript :: string recurive in javascript 
Javascript :: 8.2. Working With Arrays // Undefined 
Javascript :: $("#heading").offset({ left: left Offset }); 
Javascript :: three js div over orbitcontrol 
Javascript :: js regex replace unsafe regex 
Javascript :: use redis in adonis 
Javascript :: mapbox gl js openpopup on a marker 
Javascript :: what in the world 
Javascript :: faker link for json post req 
Javascript :: array destructuring mdn 
Javascript :: jquery ui sortable disable child 
Javascript :: Unable to load schema from https json SchemaStore org eslintrc 
Javascript :: javascript check if string contains capital letter 
Javascript :: react native return null 
Javascript :: textfield extjs retrinjir a 4 caracteres 
Javascript :: This is an example of oligosaccharides: 
Javascript :: how we use usefef in map function 
Javascript :: the caller does not have permission firestore 
Javascript :: dataForm js 
Javascript :: how to allow the onclick event of a string in javascript 
Javascript :: how to disable background when popup open in javascript 
Javascript :: fetch memes from redit from javascript 
Javascript :: javascript intl.datetimeformat brasil 
Javascript :: Mongoose make Object required 
Javascript :: how to move an array over one 
Javascript :: bookshelf log query 
Javascript :: javascript html find the largest number among 2 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =