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

时间:2016-03-19 16:56:34

标签: navigation react-native facebook-login 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,
        }} />
    );
  }
}

解决了这个问题,我使用了两次语句并将代码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 ,
        }} />
  },

1 个答案:

答案 0 :(得分:0)

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

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