onSubmit does not executed and I do not know what is problem , its execute without handleSumbit but I need it for get all data from form. I think I have done everything necessary for this form but its do not working. I need help?
import { useForm } from 'react-hook-form'; const LoginPage = () =>{ const { register, handleSubmit } = useForm(); const onSubmit = (data) => ( alert(JSON.stringify(data)) ); return( <section className="section-login"> <div className="login__header"> <img src={anarLogo} className="login__header--logo" alt="header logo" /> </div> <form className='login__form' onSubmit={handleSubmit(onSubmit)}> <Input register={register} inputName='User name' inputType='text' inputClass='form__input--text' inputPlaceholder='Username' // inputErrors = {errors} inputLabel = 'username' rules={{ required: true, maxLength: 20, min: 3 }} /> <Input register={register} inputName='Password' inputType='password' inputClass='form__input--password' inputPlaceholder='Password' // inputErrors = {errors} inputLabel = 'password' rules={{ required: true, maxLength: 20, min: 3 }} /> <button type='submit'></button> </form> </section> ) } my input file :
const Input = ({register, inputName, inputType, inputClass, inputPlaceholder, inputLabel, rules,}) => { return( <div className='form__input'> <input {...register(inputLabel , {...rules})} name={inputName} type={inputType} className={`input ${inputClass}`} placeholder={ inputPlaceholder } /> </div> ) } export default Input;
onSubmitevent the return value ofhandleSubmit(onSubmit), which makes no sense, sinceonSubmitexpects to be assigned a function, what you probably trying to achieve isonSubmit={() => handleSubmit(onSubmit)}, this will correctly execute yourhandleSubmitfunction when submittinghandleSubmit()function does return a function.onSubmit={(e) => {validateErrors();handleSubmit(onSubmit)(e);}}is what worked for me. notice the currying