Skip to content
Source
Sign inSign up
TanStack/query

`useIsMutating()` does not rerender components #3724

Send to my agent
Closedtomasbarton-stemcell(GitHub profile, opens in a new tab) opened this issue 2 comments

Describe the bug

The useIsMutating() hook does not initiate a re-render.

If there is something else that causes the component to rerender, the output of the useIsMutating() hook is correct.

This worked before 4-beta (ie worked in 4-alpha and v3) - tested by changing versions in codesandbox.

In the following example the Mutating component never renders the <div> element with the mutating count.

import "./styles.css";
import { useIsMutating, useMutation } from "react-query";

const Mutating = () => {
  const mutating = useIsMutating();
  if (!mutating) {
    return null;
  }

  return <div>Number of mutations: {mutating}</div>;
};

const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

const myMutation = async () => {
  await sleep(1000);
};

const TestMutate = () => {
  const mutation = useMutation(myMutation);
  const handleMutate = () => {
    mutation.mutate();
  };
  return <button onClick={handleMutate}>Mutate</button>;
};

export default function App() {
  return (
    <div className="App">
      <Mutating />
      <TestMutate />
    </div>
  );
}

Your minimal, reproducible example

https://codesandbox.io/s/funny-curie-2xujvt?file=/src/App.js

Steps to reproduce

Open the example and click on Mutate

Expected behavior

I expect to see "Number of mutations 1" after running a mutation that takes 1 second (simulated by a promise that resolves after 1 second).

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

codesandbox, Chrome 102

react-query version

4.0.0-beta.23

TypeScript version

4.6.4

Additional context

No response

  1. TkDodo added labelsbug

  2. TkDodo added labelsv4

  3. Thank you for reporting. We're subscribing to the wrong cache - a classic copy / paste error on my part 🙈 .

    All our tests perform top-down rendering, so this wasn't caught. I'll re-write one of the tests to make sure this doesn't happen again, and I'll push the fix soon.

  4. I've fixed this here, but It'll take some time until we can merge this due to the re-brand :)

    30c0e2ef

  5. TkDodo closed this

Sign in to join this conversation.