I use the react-hook-form library to validate my forms, but I want my page not to reload after submitting the form so that I can redirect the user to the desired page on my own. For example, using the navigate
hook from the react-router-dom library. How to stop page reloading?
My code:
import React from 'react';
import {signInWithEmailAndPassword, updateCurrentUser} from "firebase/auth";
import {auth} from "../firebase";
import {Link, useLocation, useNavigate} from "react-router-dom";
import styles from "./elements.module.css";
import {SubmitHandler, useForm} from "react-hook-form";
import {IAuthFormFields} from "../types";
import cn from "classnames";
type locationState = { from: string };
const SignIn = () => {
const navigate = useNavigate()
const location = useLocation();
const fromPage = (location.state as locationState)?.from ?? '/';
const {
register,
formState: { errors },
handleSubmit,
setError
} = useForm<IAuthFormFields>({
mode: 'onBlur'
});
const handleLogin: SubmitHandler<IAuthFormFields> = ({email, pass}) => {
signInWithEmailAndPassword(auth, email, pass)
.then(async (userCredential) => {
const {user} = userCredential;
await updateCurrentUser(auth, user);
navigate(fromPage);
});
}
return (
<form onSubmit={handleSubmit(handleLogin)}>
<fieldset className={"flex flex-col items-center"}>
<h1 className={"text-2xl font-medium"}>Sign In</h1>
<div className={"flex flex-col w-full my-3"}>
<input
type="email"
{...register('email', {
required: true,
pattern: {
value: /^[\w-.] @([\w-] \.) [\w-]{2,4}$/,
message: 'Invalid email'
}
})}
placeholder="Email"
className={cn(styles.input, "my-3")}
/>
{errors?.email && <span className={styles.msg_error} >{errors.email.message}</span>}
<input
type="password"
{...register('pass', {
required: true,
})}
placeholder="Password"
className={cn(styles.input, "my-3")}
/>
{errors?.pass && <span className={styles.msg_error} >{errors.pass.message}</span>}
<button className={cn(styles.btn, "my-3")} >
Sign In
</button>
</div>
</fieldset>
</form>
);
};
export default SignIn;
CodePudding user response:
According to documentation (https://react-hook-form.com/api/useform/handlesubmit/) - the method expects a SubmitHandler callback argument (named "a successful callback.)
((data: Object, e?: Event) => void, (errors: Object, e?: Event) => void) => Function
You have this handler, just take the event as the 2nd argument, this one:
const handleLogin: SubmitHandler<IAuthFormFields> = ({email, pass}) => {....
Will turn into this:
const handleLogin: SubmitHandler<IAuthFormFields> = ({email, pass}, e?: Event) => {
e.preventDefault()
signInWithEmailAndPassword(auth, email, pass)
.then(async (userCredential) => {....
CodePudding user response:
pass e as a parameter in form onSubmit function and inside that function write
e.preventDefault();
CodePudding user response:
And try to use at the begining of your handleSubmit function :
e.preventDefault()
CodePudding user response:
add event.preventDefault(); on the handleLogin function :) oh and you also need a event parameter