title: Redux: Why is store.getState() vs useSelector returning different values?
body: <p>I have a custom hooks I'm using to fetch values from my redux store and use it in various components.</p>
  <p>In the code below you'll see I use <code>useCustomHook</code> to return data and loading state from redux. But for some reason that value always seems to lag one render cycle.</p>
  <p>For instance, when the button is clicked I expect to see the <code>dataLoading</code> in <code>MyComponent</code> update right away, but it doesn't. When i check this against <code>store.getState().loading</code> I get the correct value. Why is that? Is there some rendering issue with react I'm not picking up here?</p>
  <pre><code>const useCustomHook = () =&gt; {
     const data = useSelector(state =&gt; state.data)
     const dataLoading = useSelector(state =&gt; state.loading)
  
     useEffect(() =&gt; {
       if (!data &amp;&amp; !dataLoading) {
          dispatch(loadMyDataAction());
       }
     }, []);
  
     const update = (newData) =&gt; {
       dispatch(updateData(newData));
     } 
  
     return {data, dataLoading, update}
  }
  
  const MyComponent = () =&gt; {
     const {data, dataLoading, update} = useCustomHook(); 
     const store = useStore();
  
     const continue = () =&gt; {
         // Here my data loading doesn't change on click of button. 
         // but value for store.getStore().loading return the right value
        while (dataLoading) {
           console.log(&quot;waiting for loading to finish&quot;); 
        }
        window.location = &quot;https://mynewsite.com&quot;
     }
  
     if (dataLoading) {
       return (&lt;div&gt;Loading&lt;/div&gt;); 
     }
  
     return (
       &lt;div&gt;
        &lt;button onClick={() =&gt; update(someRandomData)}&gt;Update&lt;/button&gt;
        &lt;button onClick={() =&gt; continue()}&gt;Continue&lt;/button&gt;
      &lt;/div&gt;
    ) 
  
  }
  </code></pre>
  <p><strong>UPDATE:</strong> So after testing the <code>dataLoading</code> in a useEffect I can see that the loading value changes in that useEffect. But in my <code>continue</code> method it doesn't.</p>