Getting 'isLoading' state undefined in react query
Asked Answered
B

1

5

After I updated react query to the latest version, I am getting undefined for 'isLoading' state when using useMutation. Here is the code:

const useAddUserNote = (owner: string) => {
  const queryClient = useQueryClient();
  const { mutate, error, data, isLoading } = useMutation({
    mutationFn: (note: CreateNoteData) => addUserNote(note),
    onSuccess: () => {
      queryClient.invalidateQueries(["allUserNotes", owner]);
    },
  });

  if (error instanceof Error) error;

  return { mutate, data, error, isLoading };
};

Here is the type error

Property 'isLoading' does not exist on type 'UseMutationResult<any, Error, CreateNoteData, unknown>'.

When I console log the 'isLoading', it is undefined

Baillargeon answered 4/11, 2023 at 20:44 Comment(0)
V
14

Take a look at the API reference for your version. In the latest documentation, isLoading no longer exists -- it appears to have been removed after version 4.

This is also explicitly called out in the upgrade guide from version 4 to version 5. Make sure to read these when you upgrade!

You should update your code to use isPending:

const { mutate, error, data, isPending } = useMutation({
Villanueva answered 4/11, 2023 at 21:34 Comment(2)
Thanks for the great help! Apparently, I have looked at the wrong place :)Baillargeon
It caches the versions u have been looking for. Due to this i never looked into he version tab :)Vestavestal

© 2022 - 2024 — McMap. All rights reserved.