Thanks very much for sharing ️
64 karma · joined September 23, 2017
1. Support React controlled component. eg some of the popular component libs, they are wrapped input inside their component and it's hard to predict which to include during the form submit.
2. React Native <3
3. Attach validation rules, messages and async validation on the individual field level.
function App() {
const { register, handleSubmit, errors } = useForm(); // initialise the hook
const onSubmit = data => {
console.log(data);
};
return (
<form onSubmit={handleSubmit(onSubmit)}>
<input name="firstname" ref={register} /> {/* register an input */}
<input name="lastname" ref={register({ required: true })} />
{errors.lastname && 'Last name is required.'}
<input name="age" ref={register({ pattern: /\d+/ })} />
{errors.age && 'Please enter number for age.'}
<input type="submit" />
</form>
);
}here is an code example, there is no local state.
I have built a custom hook for form validation, please check it out and feedback welcome <3