【发布时间】:2019-01-12 15:04:46
【问题描述】:
问题是: 如果有用户,App.js 会重定向到 MainPage,我可以得到 AsyncStorage Object
但是如果没有用户,它会重定向到登录页面并且控制台显示 Object 为空(好的,因为如果没有用户我没有设置 Object),并且当它登录时,App. Node.js 设置项目(显示在控制台中)并重定向到 MainPage(但如果我的 AsyncStorage 只显示 {})
主页
async componentDidMount() {
myArray = await AsyncStorage.getItem('userInfo');
const d = JSON.parse(myArray);
console.log('dddddddddddd:',d)
this.setState({userInfa: d})
}
App.js
componentDidMount() {
this.authListener();
}
authListener() {
firebase.auth().onAuthStateChanged(user => {
if (user) {
this.setState({ user });
const userInfo = {
username: user.displayName,
emailVerified: user.emailVerified,
photoURL: user.photoURL,
userId: user.uid
}
AsyncStorage.setItem('userInfo', JSON.stringify(userInfo));
console.log('userInfo:::::::AAAAAppp', userInfo)
} else {
this.setState({ user: null });
}
});
}
我是 react native 的新用户,实际上我不懂 componenDidMount()。希望你能帮忙
【问题讨论】:
标签: react-native components asyncstorage