title: How to redirect to homepage using React Router and React Query
body: <p>I have an app that displays a list of invoices. On clicking through to an individual invoice page, you have the option to delete that invoice. On clicking delete, a modal pops up to confirm the delete and the invoice is then removed from the list.</p>
  <p>I am using React Router to navigate the pages in the app and React Query to handle the API interaction.</p>
  <p>In the code below I am trying to delete an invoice using useMutation and invalidateQueries so that the list of invoices is updated, and then redirect to the homepage where all of the invoices are listed using Redirect.</p>
  <p>The problem I'm having is that the Redirect seems to be happening before the queries are invalidated, so when I return to the list the deleted item is still appearing until you refresh the page. Is there any way around this?</p>
  <pre><code>import React, { useState } from &quot;react&quot;;
  import { useStore } from &quot;../store&quot;;
  import { deleteInvoice } from &quot;../Api/ApiCalls&quot;;
  import { useMutation, QueryClient } from &quot;react-query&quot;;
  import { Redirect } from &quot;react-router-dom&quot;;
  
  export default function DeleteModal({ id }) {
    const [redirect, setRedirect] = useState(false);
    const { setDeleteModal } = useStore();
    const queryClient = new QueryClient();
  
    const mutation = useMutation(deleteInvoice, {
      isSuccess: () =&gt; {
        queryClient.invalidateQueries(&quot;invoices&quot;);
      },
    });
  
  const removeInvoice = () =&gt; {
      mutation.mutate(id)
      setRedirect(true)
  }
  
    return (
      &lt;div&gt;
        &lt;div className=&quot;delete-modal&quot;&gt;
          &lt;h1&gt;Confirm Deletion&lt;/h1&gt;
          &lt;p&gt;
            Are you sure you want to delete #{id}? This action cannot be undone
          &lt;/p&gt;
          &lt;button onClick={() =&gt; setDeleteModal(false)}&gt;Cancel&lt;/button&gt;
          &lt;button onClick={() =&gt; removeInvoice()}&gt;Delete&lt;/button&gt;
          {redirect &amp;&amp; &lt;Redirect to=&quot;/&quot; /&gt;}
        &lt;/div&gt;
      &lt;/div&gt;
    );
  }
  </code></pre>