每次登录都是第一个路由。
如果用户登录,我想显示选项卡页面。否则,我想导航到登录页面。
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 ,
}} />
},
希望对您有所帮助。今天是星期六,我正在努力研究我的投资组合,但我做了一些改进并澄清了一些部分。如果您有问题,请随时问我:笑脸:
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,
}} />
);
}
}