我正在Firebase为我的应用程序验证用户身份。我已经创建了SignIn和SignUp表单,并且可以成功创建新用户并使用存储的用户登录。然而,问题在于在Reload.
我在教程中看到它完成的方式是使用HOC类似以下内容来检查当前用户是否已登录。
const withAuthentication = Component => {
class WithAuthentication extends React.Component {
constructor(props) {
super(props);
this.state = {
authUser: null,
};
}
componentDidMount() {
this.listener = this.props.firebase.auth.onAuthStateChanged(
authUser => {
authUser
? this.setState({ authUser })
: this.setState({ authUser: null });
},
);
}
componentWillUnmount() {
this.listener();
}
render() {
return (
<AuthUserContext.Provider value={this.state.authUser}>
<Component {...this.props} />
</AuthUserContext.Provider>
);
}
}
return withFirebase(WithAuthentication);
};
export default withAuthentication;
但是,我希望使用 newReact Hooks来消除对HOCs. 我已经withFirebase() HOC通过使用React Context和useContext(FirebaseContext)来访问Firebase. 有没有使用新的方式hooks来模拟这个withAuthentication HOC范围内components,我创造?
我正在使用本教程
https://www.robinwieruch.de/complete-firebase-authentication-react-tutorial/
标题为“使用高阶组件进行会话处理”的部分包含此部分。
谢谢!