In angular2, if we want to implement form validation we will use Directives. In React, how can we achieve this validations ???
3 Answers
There are several ways:
- Create your own validation from scratch with plain javascript
- Use browser APIs
- Or you can use 3rd party plugins like 'ract-validation': https://github.com/Lesha-spr/react-validation
Comments
After struggling with too many libraries on web, here is the one I have found more helpful in entire react world. It can make developers more productive by simplifying validation job.
Try using : Formik
npm install formik --save
Why you should use : It will saves you writing too much boggy JSX (involving annoying properties) and dealing directly with underline handling (writing own
onBlur()onChange()event handlers) just to achieve validation.
Formik is a small library that helps you with the 3 most annoying parts:
- Getting values in and out of form state
- Validation and error messages
- Handling form submission
By colocating all of the above in one place, Formik will keep things organized--making testing, refactoring, and reasoning about your forms a breeze.
Comments
You should try Formik. I am currently working with it in a project and it goes quite well.
As per their readme.md in github they say: "Formik is a small library that helps you with the 3 most annoying parts:
Getting values in and out of form state Validation and error messages Handling form submission By colocating all of the above in one place, Formik will keep things organized--making testing, refactoring, and reasoning about your forms a breeze."