title: Wait for localStorage update before dispatching fetch on useEffect
body: <p>I have a login page which returns a response , after which im setting a token in localStorage.</p>
  <pre><code>  const login = async (e) =&gt; {
      e.preventDefault();
      try {
        const loginRes = await axios.post(`${url}/users/login/`, {
          username,
          password,
        });
        if (loginRes.data?.success === 1) {
          clearInputs();
          localStorage.setItem(&quot;user_id&quot;, loginRes.data.user_id);
          localStorage.setItem(&quot;token&quot;, loginRes.data.token);
          history.push(&quot;/home&quot;);
        } else {
          toast(loginRes.data?.data);
        }
      } catch (error) {
        toast(&quot;An error occurred, please try again&quot;);
      }
    };
  </code></pre>
  <p>Im redirecting to home page after the response as:</p>
  <pre><code>    history.push(&quot;/home&quot;);
  </code></pre>
  <p>Inside home page im dispatching the action to get data from api,</p>
  <pre><code>  useEffect(() =&gt; {
      if (!localStorage.getItem(&quot;token&quot;)) {
        history.push(&quot;/&quot;);
        return;
      }
      dispatch(getItems())
    }, [history, dispatch]);
  
  
  export const getItems = () =&gt; {
      return async(dispatch) =&gt; {
          dispatch(fetchItemsRequest());
          await api.fetchItems().then(
              (res) =&gt; {
                  dispatch(fetchSuccess(res.data.items));
              },
              (error) =&gt; {
                  dispatch(fetchFailed(error));
              }
          );
      }
  };
  </code></pre>
  <p>For the fetchItems() request, it requires the token from localStorage.</p>
  <p>The problem is that when i click login , it is redirecting the home page and on the first load, token is passed as null , and no items are loaded in the home page as token is invalid. When i refresh the page, the data loads correctly. How do i wait for the localStorage to be set before the useEffect is called from home page?</p>