-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Description
Describe the bug
Imagine creating a custom hook (let's name it useTodos
) which is used in multiple components and uses useQuery
and useMutation
inside.
When using useQuery
, its status (and the helpers isLoading
, isError
, ...) are shared across multiple usages of useTodos
.
For useMutation
, this is not true. Every usage of useTodos
will have its own mutation status.
To Reproduce
Here's a CodeSandbox that shows the behavior.
It shows two components that both use useTodos
.
For GET requests (via useQuery
), the status is shared across the components.
For POST requests (via useMutation
), the status is different for both components. Only the component who executes the mutation will have the status "error".
Expected behavior
I understand that useQuery
has one precise query key to identify "the same hook". Mutations are not like that.
In my case, I had a list that showed items (like "Todos") and a child component that was supposed to create a new item (like "Todo"). Naturally, I used useTodos
in both components and wanted to show an error in the List component. The problem is isError
was never true
, only the child component's one was.
function TodoList() {
// this "isError" here will never be true, only the <Todo> component's "isError" will
const { todos, isError } = useTodos();
return (
<div>
<p>Todos:</p>
{isError && <p>Error!</p>}
<div>
{todos.map((todo) => (
<Todo todo={todo} />
))}
</div>
</div>
);
}
function Todo({ todo }: { todo: Todo | null }) {
const { createTodo } = useTodos();
return (
<div>
{todo ? (
<p>{todo.title}</p>
) : (
<div>
<button onClick={() => createTodo(...)}>Create</button>
<input ... />
...
</div>
)}
</div>
);
}
I'm using React Query for a while now and being used to useQuery
having a shared status, I wasted time checking my mutation implementation ("Why is the error not thrown?" etc.), but in reality useQuery
and useMutation
just behave differently and differently than expected.
I would really like to let useMutation
s share their status across all components.
There's already a mutationKey
, which could maybe used for this exact use case.