title: Try refresh token and invalidate query when react-query encounters 401
body: <p>I would like to create a wrapper hook around react-query's <code>useQuery</code> hook so that I could catch a 401 error, attempt to refresh access token and if successfully refreshed - invalidate the original query.</p>
  <p>Full example of what I am trying to do is here: <a href="https://codesandbox.io/s/agitated-booth-hbe12?file=/src/App.js" rel="noreferrer">https://codesandbox.io/s/agitated-booth-hbe12?file=/src/App.js</a></p>
  <pre class="lang-js prettyprint-override"><code>function useMyQUery() {
    const queryClient = useQueryClient();
    const { tryRefreshToken } = useSession();
  
    const query = useQuery(...arguments);
  
    if (query.isError &amp;&amp; query.error?.status === 401) {
      tryRefreshToken().then((tokenRefreshSucccessful) =&gt;
        queryClient.invalidateQueries(&quot;todos&quot;)
      );
    } else {
      return query;
    }
  
    return {};
  }
  </code></pre>
  <p>In the example I have linked above I am able to catch the error, trigger the function <code>tryRefetchToken</code>, but the functions inside the <code>useSession</code> hook do not seem to pick up the token once it is set.</p>