Hi guys, I have built a custom hook for form validation, please check it out and feedback welcome <3
I've been playing with it for the past ~40 minutes and i really like it. Can't wait to use it on a real project soon. In fact, i might start converting some older things to it in spare time. The website and docs are absolutely amazing, best i've seen in a long time. Thank you for doing this!
Show HN: React Hook Form – Simple Form Validation
11–20 of 75 posts
Re: Show HN: React Hook Form – Simple Form Validation
#12Earlier quoted context omitted.
Great thanks. One other helpful thing would be to start a project full of pre-wrapped components for various frameworks. It would be lovely to just yarn add something that would give me Material-UI components already set up. I'm sure others would love a Bootstrap version... etc. The more you can lower the barriers to entry, the better.
By the way I did have a folder of examples as well. Quite lot of them: https://github.com/react-hook-form/react-hook-form/tree/mast... with codesandbox link too
While it didn't take me that long to do, it does have some weird edge cases that I had to google around for. It would be nice to not have to figure this stuff out with your framework too and would encourage me to try it out more quickly.
I know that formik and redux-forms are popular, but react-final-form and your framework seem to have a lot of overlap. Both seem to be pretty well done.
[1] https://gist.github.com/lookfirst/558fb2161fd633c7eaab412b85...
Re: Show HN: React Hook Form – Simple Form Validation
#13for example, after a form submit event, you can do
const elements = [...e.target.elements].filter(el => el.name); // all form elements with a name
// invoke onSubmit prop with form data:
onSubmit(
elements.reduce((o, el) => ({
...o,
[el.name]: el.type==='checkbox' ? el.checked : el.value
}), {})
);Re: Show HN: React Hook Form – Simple Form Validation
#14I've never seen a benefit in storing a form state in react state, forms already hold a state in the DOM, you can read, update and validate it ( https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Fo... ) for example, after a form submit event, you can do const elements = [...e.target.elements].filter(el => el.name); // all form elements with a name // invoke onSubmit prop with form data: onSubmit( elements.redu…
Re: Show HN: React Hook Form – Simple Form Validation
#15 class MyForm extends React.Component {
formState = new ReduxHookForm();
render() {
const { register, handleSubmit } = this.formState;
return (
male
female
);
}
handleSubmit(data) {
console.log(data);
}
}Re: Show HN: React Hook Form – Simple Form Validation
#16This looks really good. But is there a non-hook version for use in class components? I'm imagining it might look like this: class MyForm extends React.Component { formState = new ReduxHookForm(); render() { const { register, handleSubmit } = this.formState; return ( male female ); } handleSubmit(data) { console.log(data); } }
Re: Show HN: React Hook Form – Simple Form Validation
#17I've never seen a benefit in storing a form state in react state, forms already hold a state in the DOM, you can read, update and validate it ( https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Fo... ) for example, after a form submit event, you can do const elements = [...e.target.elements].filter(el => el.name); // all form elements with a name // invoke onSubmit prop with form data: onSubmit( elements.redu…
function App() {
const { register, handleSubmit, errors } = useForm(); // initialise the hook
const onSubmit = data => {
console.log(data);
};
return (
{/* register an input */}
{errors.lastname && 'Last name is required.'}
{errors.age && 'Please enter number for age.'}
);
}here is an code example, there is no local state.
Re: Show HN: React Hook Form – Simple Form Validation
#18This looks really good. But is there a non-hook version for use in class components? I'm imagining it might look like this: class MyForm extends React.Component { formState = new ReduxHookForm(); render() { const { register, handleSubmit } = this.formState; return ( male female ); } handleSubmit(data) { console.log(data); } }
yea would be similar for class component :)
Re: Show HN: React Hook Form – Simple Form Validation
#19This looks really good. But is there a non-hook version for use in class components? I'm imagining it might look like this: class MyForm extends React.Component { formState = new ReduxHookForm(); render() { const { register, handleSubmit } = this.formState; return ( male female ); } handleSubmit(data) { console.log(data); } }