内容简介:Creating forms in a React app is a real pain. We need to handle all the value changes ourselves. For inputs text boxes, we need to get the value and update the state with our own code. For other form controls like checkboxes and radio buttons, it’s even ha
How to speed up your React form development with Formik.
Apr 30 ·5min read
Creating forms in a React app is a real pain. We need to handle all the value changes ourselves. For inputs text boxes, we need to get the value and update the state with our own code. For other form controls like checkboxes and radio buttons, it’s even harder.
One library that helps us make our lives easier is Formik . In this article, we’ll look at how to use Formik in our React app to create forms.
“Uncommon thinkers reuse what common thinkers refuse.” — J. R. D. Tata
Regardless of which library you use to build your forms, make sure you publish your UI components to a cloud component hub like Bit.dev. Use it to share, document and organize your reusable code. It doesn't have to be a complete ‘library project’ — you can publish individual components from any codebase, whenever you feel like it.
Just like using form libraries, it’s another step towards a faster and more pleasant development, for you and your team.
Getting Started
To get started, we first have to install the Formik NPM package.
We do that by running:
npm i formik
Next, we can start creating our form.
To do that, we include the Formik
component in our app, which we can use by nesting our form control components inside it.
Then the state will update without us writing much code.
To create a simple form, we can write:
In the code above, we added the Formik
component, which has various props.
The initialValue
prop lets us specify the initial values of the form fields.
The validate
prop lets us specify validation rules.
It’s a function that takes the values and let us return an errors
object with the key being the key of the form value of the value being the message.
onSubmit
is a prop that takes a function when we click Submit.
All we do is show an alert box with the values that are inputted to keep things simple for this example.
The setSubmitting
prop is a function that's called to let Formik know if we're submitting or not.
If we’re done, then we pass in false
to setSubmitting
as we did above.
The function we pass into the onSubmit
prop won't be called until all the form fields have valid data.
Inside the Formik
component, we have a function with an object parameter with lots of properties.
And we return our form element, which has 2 input text controls.
The handleChange
parameter has the change handler that we need to handle any form value changes.
The handleSubmit
has our on-submit handler which we defined in the onSubmit
prop.
handleBlur
has the onBlur
handler for the form element.
We display the error objects after the input control is touched and the error
object is populated.
The isSubmitting
is also a useful parameter that lets us know whether the form is being submitted or not.
If it’s being submitted, then we display the Submit button so that we can’t submit again.
values
has the form value which we can set as the value of the value
prop of our input elements.
Making Things Simpler.
Fortunately, we don’t have to write all that code explicitly every time.
Formik provides us with the Form
, Field
, and ErrorMessage
components to let us build forms, add form fields, and display error messages respectively.
We can simplify our previous example by writing:
This component is at https://bit.dev/jauyeunggithub/formik-simple/app
We simplified it greatly with the Form
, Field
, and ErrorMessage
components that come with Formik.
The fields are in the Form
component.
The Field
component has our form inputs.
And the ErrorMessage
components have the messages we're displayed.
The only left in the function that we passed inside the Formik
component is the isSubmitting
property from the parameter.
We still used it to set the disabled
prop of the button as we did with the previous example.
Hooks API
Like most popular React libraries, Formik also comes with a hook-based API.
Formik has the useFormik
hooks, which returns a function that we can use in our own forms for handling form value changes, displaying error messages, and form submission.
We can use the useFormik
hook as follows:
This component is at https://bit.dev/jauyeunggithub/formik-hooks/app
We separated out the validate
function by moving it outside the App
component.
The validation mechanism is the same. We check the value of the form fields that are specified as keys of the initialValue
property.
In App
, we called the useFormik
hook by passing an object with the initialValues
property, our validate
function, and our onSubmit
function, which will be called if all the inputted values are valid.
It returns the formik
object, which has the handleSubmit
, handleChange
, errors
and values
properties, which are all the same as the previous examples.
The benefit of using useFormik
is that we can move our validate
outside of our component.
Also, the return methods and properties can be used however we like in our forms.
This means that we don’t have to write out everything explicitly and we don’t have to use the components that are built into Formik, which are quite restrictive.
We have clean syntax and flexible at the same time with the hooks API.
Conclusion
Formik is an easy to use library that lets us add forms to a React with form validation without the hassle of handling form values ourselves.
Also, it will return form validation errors automatically so that we don’t have to deal with them ourselves.
We just put the ones that are returned wherever we like.
The hooks that come with Formik give us more flexibility with how we can create our forms in a React app.
以上所述就是小编给大家介绍的《Creating React Forms with Formik》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
公众号运营实战手册
中信出版社 / 2018-11 / 58
作者粥左罗在刚入行做新媒体的一年时间里,就写了100篇阅读量10万+的公众号文章,但是在此之前,他足足花了两个月的时间研究公众号运营和爆款文章的逻辑和打法。 这本书就是他总结和归纳自己公众号写作和运营的全部秘诀和技巧,是一本行之有效的实战指南。 从如何注册一个公号,给公号起什么名字? 多长时间更新一次为好? 到如何找选题,如何积累爆款素材? 如何编辑内容,如何做版面设......一起来看看 《公众号运营实战手册》 这本书的介绍吧!