[英]User information get lost after refreshing the page (Firebase + React)
I am trying to create a Navigation bar. 我正在尝试创建一个导航栏。 When the user is not logged in, they see a Navigation bar, with the title of the page only.
当用户未登录时,他们会看到导航栏,仅显示页面标题。 Once the user logs in, he should see the title, other pages, his email address, and a logout button.
一旦用户登录,他应该看到标题,其他页面,他的电子邮件地址和注销按钮。
I can retrieve the email address, but when I refresh the page and click the logout button I get an error saying: 我可以检索电子邮件地址,但是当我刷新页面并单击注销按钮时,我收到一条错误消息:
TypeError: Cannot read property 'email' of null TypeError:无法读取null的属性“email”
I am attaching my code for the navigation bar and how I can it in app.js, down below. 我正在附加导航栏的代码以及如何在app.js中将其添加到下面。
I am not sure how can I tackle this problem. 我不知道如何解决这个问题。
NavigationBar.js: NavigationBar.js:
const NavigationBar = ({ authUser }) => (
<div>{authUser ? <NavigationBarAuth /> : <NavigationBarNonAuth />}</div>
);
const NavigationBarAuth =()=>{
return (
...
<Grid item xs={3}>
<div className ="nav-user">
<a className = "font user">{fire.auth().currentUser.email}</a>
</div>
</Grid>
);
App.js: App.js:
constructor(props) {
super(props);
this.state = {
authUser: null,
// authUser: JSON.parse(localStorage.getItem('authUser')),
};
}
componentDidMount() {
this.listener = fire.auth().onAuthStateChanged(
authUser => {
authUser
? this.setState({ authUser })
: this.setState({ authUser: null });
},
);
}
componentWillUnmount() {
this.listener();
}
render() {
return (
...
<NavigationBar authUser={this.state.authUser} />
);
}
After debugging, I understood the problem was somewhere else. 经过调试,我明白问题出在其他地方。 I had a Signout function and called the function in button onClick.
我有一个Signout函数,并在按钮onClick中调用该函数。 I called the function wrongly which is why the signout function got fired and nullified the user.
我错误地调用了函数,这就是为什么注销函数被触发并使用户无效的原因。 Thanks for your help and effort.
感谢您的帮助和努力。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.