skip to Main Content
const [login] = useLoginMutation({
        onSuccess: () => {
            console.log("Success");
            setAlertType("success");
            setAlertMessage("Login successful!");
            setOpenAlert(true);
        },
        onError: (error) => {
            console.log("Error: " + error);
            setAlertType("error");
            setAlertMessage(`Login failed: ${error}`);
            setOpenAlert(true);
        }
    });

Everything is working except that the onSuccess and onError doesn’t fire. The query is successful and everything.

2

Answers


  1. Chosen as BEST ANSWER
    const submitLoginForm = async (e) => {
        e.preventDefault();
    try {
        const response = await login(loginData);
        
        if(response.error) {
            setAlertType("error");
            setAlertMessage("Incorrect username or password");
            setOpenAlert(true);
        } else if (response.data) {
            setAlertType("success");
            setAlertMessage(response.data.message);
            setOpenAlert(true);
        }
    
        dispatch(hide());
        refetchLogin();
    } catch (err) {
        console.log(err);
    }
    

    }


  2. onSuccess and onError are not options in RTK Query.

    You can not just make up options and expect them to work. Maybe you confused that with options from another library?

    Generally, the hooks do not have any lifecycle callbacks. You can defined lifecycle callbacks (different ones, with different names) on endpoint level, but not on component/hook level.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search