Forms are hard...
Let's start with descriptive quotes by well known developers in React community.
“Forms are hard.”
Jared Palmer - author of Formik
“Forms are hard.”
Eric Rasmussen - author of redux-form and final-form {.h3}
“React exposed the inherent complexity of form interaction that was always there.”
Jordan Walke - React & ReasonML core developer {.h3}
Yes, you should be very careful when it comes to form development. Because form user interface is not just about clicking on some elements. The user usually uses a keyboard, hitting keys several times per second, and the form needs to effectively process those changes and render form data accordingly to those rapid state changes.
And React's unidirectional data
flow does not make it easier either. When form values are stored in a central state, every state change leads to calling a render
function. So, in larger forms (20+ input fields) you should make sure, that you do not re-render whole form upon each change. Because if do ... you may notice performance issues in the form of lags. And users do not like lags.
Another problem is that there are so many different states the form can actually have.
What you need to know from a form state:
- What were initial values of fields?
- What are the current values of fields?
- What field is focused right now?
- Which fields were already visited by the user?
- Which fields contain warnings or validation errors?
- Is there an asynchronous validation running?
- Is the form being submitted at the moment?
- What was the result of form submission?
In this article we will make a research about the most used form libraries for React. Before we move to next parts of this series, which will cover a Formik-based implementation, I want to share my custom implementation of an alternative - a simple and reusable