title: How to Logout (Go to Base URL) if local Storage doesn't have certain key-value pair in React
body: <p>I am implementing a simple logout functionality if my local storage doesn't have a particular key-value pair and if the value is empty or if the 'token' inside the value is expired.</p>
  <p>My current Code: TokenExpired.js</p>
  <pre><code>import { isExpired } from &quot;react-jwt&quot;;
  import { useNavigate } from &quot;react-router-dom&quot;;
  
  export const VerifyAccessToken = () =&gt; {
      const navigate = useNavigate()
      const Data = localStorage.getItem('Admin Credentials')
      const existanceOfData = Data !== null
  
      if (existanceOfData) {
          if (Data &amp;&amp; Data !== 'undefined') {
              const tokenExpired = isExpired(JSON.parse(Data).accessToken);
              if (tokenExpired) {
                  localStorage.removeItem(&quot;Admin Credentials&quot;);
                  navigate('/')
              }
          } else {
              localStorage.removeItem(&quot;Admin Credentials&quot;);
              navigate('/')
          }
      } else {
          navigate('/')
      }
  
  }
  </code></pre>
  <p>I am using this in My Dashboard Page : Dashboard/Dashboard/js</p>
  <pre><code>import &quot;./Dashboard.scss&quot;;
  import { adminAuth } from &quot;../../helpers/AdminInformation&quot;;
  import { VerifyAccessToken } from &quot;../../helpers/TokenExpired&quot;;
  
  // components ---------------------------------
  certain components
  
  import { useEffect, useState } from &quot;react&quot;;
  
  const Dashboard = () =&gt; {
      const [dashboard, setDashboard] = useState({ received: 0, expected: 0 })
      const token = adminAuth.accessToken;
      
      useEffect(() =&gt; {
          fetch(baseURL + 'api/dashboard/', {
              headers: {
                  token: `Bearer ${token}`
              }
          }).then(res =&gt; res.json()).then(json =&gt; setDashboard(json));
      }, [token])
  
      VerifyAccessToken();
  
      return (
          &lt;div className=&quot;dashboard&quot;&gt;
              content
          &lt;/div&gt;
      );
  }
  
  export default Dashboard;
  </code></pre>
  <p>Whenever I try to delete that key value after logging in, it shows error:
  <a href="https://i.stack.imgur.com/OHUpQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OHUpQ.png" alt="enter image description here" /></a></p>