title: Best way to check if there is already a token in local storage using use effect in React Context
body: <p>good day. Is this the best way to check if there is already a token in my local storage in my AuthContext? I used a useEffect hook in checking if the token already exists. Should I change the initial state of isAuthenticated since it is always false upon rendering? Im not sure. Thank you</p>
  <pre><code>import React, { useState, useContext, useEffect } from &quot;react&quot;;
  
  const AuthContext = React.createContext();
  
  export function useAuth() {
      return useContext(AuthContext);
  }
  
  export const AuthProvider = ({ children }) =&gt; {
      const [isAuthenticated, setAuthenticated] = useState(false);
  
      const login = () =&gt; {
          setAuthenticated(true);
      };
  
      useEffect(() =&gt; {
          const token = localStorage.getItem(&quot;AuthToken&quot;);
          if (token) {
              setAuthenticated(true);
          } else if (token === null) {
              setAuthenticated(false);
          }
  
          return () =&gt; {};
      }, []);
  
      return &lt;AuthContext.Provider value={{ isAuthenticated, login }}&gt;{children}&lt;/AuthContext.Provider&gt;;
  };
  
  </code></pre>