尝试/dashboard在 Firebase 身份验证后查看用户页面,但每次都发生错误。从根和使用<Link to={"/dashboard"}是工作。但直接 url 不起作用。标头调用firebase.currentUser(),但它不能。
错误:
TypeError:无法读取 null 的属性“uid”
编码:
auth.js
class Auth extends Component {
constructor(props) {
super(props);
this.state = {
loading: true
};
}
componentDidMount() {
firebase.auth().onAuthStateChanged(user => {
if (!user) {
this.props.history.push("/");
} else {
this.setState({ loading: false });
}
});
}
render() {
return (
<div>
{this.state.authenticating ? (
<div className="loader">
<Loader loading={this.state.loading} />
</div>
) : (
this.props.children
)}
</div>
);
}
}
header.js
class Header extends Component {
render() {
const user = firebase.auth().currentUser;
return (
<p>{user.uid}</p>
)
}
}
dashbboard.js
class Dashboard extends Component {
render() {
return (
<div>
<Auth>
<p>Dashboard</p>
</Auth>
</div>
)
}
}