如何回调函数结果在本机反应中确定的初始路线。?

How to callback function result initial route determined in react native.?

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

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

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,
        }} />
    );
  }
}

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

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 ,
        }} />
  },

希望对您有所帮助。今天是星期六,我正在努力处理我的投资组合,但我做了一些改进并澄清了一些部分。如果您有任何问题,请随时问我 :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,
        }} />
    );
  }
}