title: How do I use the Firebase onAuthStateChange with the new React Hooks?
body: <p>I am using <code>Firebase</code> to authenticate users for my application. I have created the <code>SignIn</code> and <code>SignUp</code> forms and can successfully create new users and sign in with stored users. However the issue comes with maintaining the user logged in state after a <code>Reload</code>.</p>
  
  <p>The way I have seen it done in tutorials is to use a <code>HOC</code> like the following to check if the current user is logged in.</p>
  
  <pre><code>const withAuthentication = Component =&gt; {
    class WithAuthentication extends React.Component {
      constructor(props) {
        super(props);
  
        this.state = {
          authUser: null,
        };
      }
  
      componentDidMount() {
        this.listener = this.props.firebase.auth.onAuthStateChanged(
          authUser =&gt; {
            authUser
              ? this.setState({ authUser })
              : this.setState({ authUser: null });
          },
        );
      }
  
      componentWillUnmount() {
        this.listener();
      }
  
      render() {
      return (
        &lt;AuthUserContext.Provider value={this.state.authUser}&gt;
          &lt;Component {...this.props} /&gt;
        &lt;/AuthUserContext.Provider&gt;
        );
      }
    }
  
    return withFirebase(WithAuthentication);
  };
  
  export default withAuthentication;
  </code></pre>
  
  <p>However I am looking to use the new <code>React</code> <code>Hooks</code> to remove the need for <code>HOCs</code>. I have already removed the <code>withFirebase()</code> <code>HOC</code> by using the <code>React Context</code> and <code>useContext(FirebaseContext)</code> to access a single instance of <code>Firebase</code>. Is there a way using the new <code>hooks</code> to mimic this <code>withAuthentication</code> <code>HOC</code> within <code>components</code> that I create?</p>
  
  <p>I am using this tutorial </p>
  
  <p><a href="https://www.robinwieruch.de/complete-firebase-authentication-react-tutorial/" rel="noreferrer">https://www.robinwieruch.de/complete-firebase-authentication-react-tutorial/</a></p>
  
  <p>The section titled "Session Handling with Higher-Order Components" contains this part.</p>
  
  <p>Thanks!</p>