title: How to check if user is logged by localStorage and Redirect according to it?
body: <p>I am new to localStorage and React Router, and my goal is:
  Redirect user to the &quot;/dashboard&quot; when he is logged in, and Redirect back to '/home' when he is logged out. Also, of course, not allowing him to go to the 'dashboard' if he is not logged in. For some reason my code in App.js not working:</p>
  <pre><code>function App() {
    let userLogged;
  
    useEffect(() =&gt; {
      function checkUserData() {
        userLogged = localStorage.getItem(&quot;userLogged&quot;);
      }
  
      window.addEventListener(&quot;storage&quot;, checkUserData);
  
      return () =&gt; {
        window.removeEventListener(&quot;storage&quot;, checkUserData);
      };
    }, []);
  
    return (
      &lt;div className=&quot;App&quot;&gt;
        &lt;React.StrictMode&gt;
          &lt;Router&gt;
            &lt;Routes&gt;
              &lt;Route path=&quot;/&quot; element={&lt;Home /&gt;} /&gt;
              {userLogged ? (
                &lt;Route path={&quot;/dashboard&quot;} element={&lt;Dashboard /&gt;} /&gt;
              ) : (
                &lt;Route path={&quot;/home&quot;} element={&lt;Home /&gt;} /&gt;
              )}
            &lt;/Routes&gt;
          &lt;/Router&gt;
        &lt;/React.StrictMode&gt;
      &lt;/div&gt;
    );
  }
  
  export default App;
  
  </code></pre>
  <p>I set it in the home and dashboard pages by <code>localStorage.setItem('userLogged', false)</code> and <code>localStorage.setItem('userLogged', true)</code></p>