如何回调函数结果初始路由在本机中确定。

时间:2021-12-13 19:38:45

Login is the first route each time.

登录是每次的第一条路线。

I want to show the tabpage if user login. Otherwise, I want to navigate to the login page.

如果用户登录,我想显示标签页。否则,我想导航到登录页面。

class blabla extends Component {
  constructor(props) {
      super(props);
      this.state = {
        user: null
      }
  }
  componentDidMount(){
     var _this = this;
      FBLoginManager.getCredentials(function(error, data){
        if (!error) {
          _this.setState({ user : data.credentials });
        } else {
          _this.setState({ user : null });
        }
      });
  }
  render() {
  console.log(this.state.user)
  // 1. render -> null 
  // 2. render -> data 
    return (
      <NavigatorIOS ref="navigator"
        style={styles.nav}
        initialRoute={{
          title: "blabla",
          component: this.state.user != null  ? TabPage : Login,
        }} />
    );
  }
}

Solved this problem , I use twice statement and replace the code es6 syntax to es5 because I don't know use if( !_this.isMounted() ) return; in es6 syntax .

解决了这个问题,我使用了两次语句并将代码es6语法替换为es5因为我不知道使用if(!_this.isMounted())返回;在es6语法中。

getInitialState: function(){
    return {
      credentials: undefined,
      load:true
    };
  },


  componentDidMount: function(){
    var _this = this;
    FBLoginManager.getCredentials(function(error, data){
      if( !_this.isMounted() ) return;
      if (!error) {
        _this.setState({ credentials : data.credentials,load:false });
      } else {
        _this.setState({ credentials : false,load:false });
      }
    });
  },

  render: function() {
    if (this.state.load) {
      return <View style={{flex:1,justifyContent:"center"}}><Text style={{textAlign:"center"}}>loading...</Text></View>
    }
    return <NavigatorIOS ref="nav"
        style={{flex:1}}
        navigationBarHidden={false}
        rightButtonTitle="Profile"
        onRightButtonPress= {() => this.left()}
        initialRoute={{
          title: "AnsApp",
          component: this.state.credentials != undefined && this.state.credentials != false ? TabPage : Profile ,
        }} />
  },

1 个解决方案

#1


0  

Hope this helps you. It's saturday and I'm working hard on my portfolio, but I did some improvements and clarified some parts. Feel free to ask me if you have questions :smiley:

希望这对你有所帮助。这是星期六,我正在努力研究我的投资组合,但我做了一些改进并澄清了一些部分。如果您有任何疑问,请随时问我:笑脸:

 class blabla extends Component {
  constructor(props) {
      super(props);
      this.state = {
        user: null ,
        loggedInn: !true
      }
  }
  componentDidMount(){
     var _this = this;
      FBLoginManager.getCredentials(function(error, data){
        if (this.state.loggedInn = !true) {
          _this.setState({ user : data.credentials });
        }
           return this.state;
      });
  },
  Logon () {
   // your this.state validation 
  }
  render() {
  console.log(this.state.user)
  // 1. render -> null 
  // 2. render -> data 
    return (
      <NavigatorIOS ref="navigator"
        style={styles.nav}
        initialRoute={{
          title: "blabla",
          component: this.state == Logon ? TabPage : Login,
        }} />
    );
  }
}

#1


0  

Hope this helps you. It's saturday and I'm working hard on my portfolio, but I did some improvements and clarified some parts. Feel free to ask me if you have questions :smiley:

希望这对你有所帮助。这是星期六,我正在努力研究我的投资组合,但我做了一些改进并澄清了一些部分。如果您有任何疑问,请随时问我:笑脸:

 class blabla extends Component {
  constructor(props) {
      super(props);
      this.state = {
        user: null ,
        loggedInn: !true
      }
  }
  componentDidMount(){
     var _this = this;
      FBLoginManager.getCredentials(function(error, data){
        if (this.state.loggedInn = !true) {
          _this.setState({ user : data.credentials });
        }
           return this.state;
      });
  },
  Logon () {
   // your this.state validation 
  }
  render() {
  console.log(this.state.user)
  // 1. render -> null 
  // 2. render -> data 
    return (
      <NavigatorIOS ref="navigator"
        style={styles.nav}
        initialRoute={{
          title: "blabla",
          component: this.state == Logon ? TabPage : Login,
        }} />
    );
  }
}