2

I am using a React Query mutation to make a server side request, however, the isLoading property does not change when there is an axios error (500 server side error) and the isError property also stays false. For reference, I have the following setup.

Base HTTP client class:

export class HttpClient {
  static async post(url, data, options) {
    const response = await Axios.post(url, data, options);

    return response.data;
  }
}

Mutation:

export const useRegisterMutation = () => {
  const queryClient = useQueryClient();
  return useMutation((vars) => {
    return HttpClient.post(API_ENDPOINTS.REGISTER, vars);
  }, {
    onSuccess: () => {
      toast.success('Your account was successfully created!');
    },
    // Always refetch after error or success:
    onSettled: () => {
      queryClient.invalidateQueries(API_ENDPOINTS.REGISTER);
    },
    onError: (error) => {
      toast.error('There was a problem while creating your account. Please try again later!')
    }
  });
};

Usage:

const { mutate: registerUser, isLoading, isError } = useRegisterMutation();
async function onSubmit({ email, password }) {
    registerUser(
      {
        email,
        password,
      },
      {
        onSuccess: (data) => {
          // set login creds
        },
        onError: (error) => {
          // set form errors
        },
      }
    )
  }

Problem

With this setup, isLoading stays true and isError remains false even if Axios throws an error due to a server side 500 error.

Tried

I:

  • Updated the library version.
  • Checked whether the code is executed in the right place.
  • Lost all hope.
3
  • You're not actually setting isLoading or isError anywhere - or am I completely missing something? Commented Jan 5, 2023 at 15:44
  • @YestheCia they're provided by the useRegisterMutation hook. Commented Jan 6, 2023 at 8:38
  • code looks correct. please show a codesandbox or stackblitz reproduction Commented Jan 15, 2023 at 14:46

1 Answer 1

1

I used to have the same problem, but in a useQuery hook. Turned out it was because of the retries - if you set the retry flag to zero, you'll have the error flag set to true instantly. Otherwise, the error will show up by the end of the retry period, which by default I think it is 3 attempts * 5 minutes each.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.