visit
# How does the component behave?# Where do i keep my data?# How do i perform validations?# How do i detect the form’s state and changes?
At , a lot of our pages in our service are form based, it has been for a while so we are aware of the challenges and we addressed the items above already when we used Backbone.js, but hey, React is a different playground and we needed on one hand make it seamless to the user to fill the form, whether he is using our backbone forms or our React ones, but in the same time completely redesign the whole way we use forms and answer these questions all over again. before we start answering the questions, let me start from the end, we used — a form state management library on top of redux, Created by , a highly maintained library that changes and evolves on a weekly basis.#How Does My Component Behave?
Every form component in our system has to have a user-interface behavior. Take an input for instance, it has many states: is valid, is touched, is focused, is dirty, and many more states. for every state you want to have a visual representation of this state. display errors, warning etc. We chose Redux-form’s solution for that, which provides meta information about the state of your component. How do I use a redux form component? The <Field/> component is how you connect each individual input to the Redux store (I will elaborate on this subject in my next point) The only thing you need to do to activate it, is to wrap you component with a <Field/> component as displayed above, provide it with a name, your component and then you can display UI states as you wish. There are several ways to generate a redux form <Field/>, you can check the to see. So now you can add your UI for each state as you wish. As you see above, when you use the <Field/> you get for free UI states, so i can display a different notification based on a different state.
One of our main questions we had to answer was where to keep the data. Let me elaborate what were our concerns that we needed to answer: Every form element must have an initial value, it can be an empty one in case your form is acting in its “new” state, or some sort of value for an “edit” state. On any change\blur\keyUp (or any other event that you are interested in) you want the value to update. So where do i keep it?
Redux-form’s solution, which is — A Redux reducer that listens to dispatched redux-form actions to maintain your form state in Redux. Your forms will be mounted in the “form” key name , and every form will have a unique name, and accessing it is as simple as form[name]. below you can see how easy it is to set it up. redux-form provides selectors for you initial values and your current values. A great thing about using it is that its just redux, you can listen to specific custom action and manipulate your data accordingly, not only on redux-form actions but for your specific actions. . So we have talked so far about Displaying the current state of the component, and about where we manage the data. But what about handling the data itself?# How Do I Perform Validations?
Validation using Validator.Js In the example above you can see a validation example. You can provide several validation on each field (e.g: input), also you can provide the text for each error and once you run the validation you can return the error object.
#How To Detect Form’s State and Changes
Our last big item we needed to resolve was to detect the current state of the form, and to track its changes. I will give an example — We wanted to detect whether for has changed from its original values, so that when the user tries to navigate we can notify him that he has unsaved changes. Redux Form provides the full visibility of the current form state. You can detect that its dirty, pristine, was it even touched at all by the user (a focus on an element) and more. It also provide an outer API allowing you to trigger state changes you want through redux actions, for example you can submit your form, or reset your form using a redux action no need for an actual submit button if not needed.In Conclusion
Forms is always tricky, and Yes, every form has its needs, and sometimes you need to find solutions and work arounds, but most on the scenarios are covered by redux-form, and if they aren’t you can easily migrate you solution to work with redux form. For us it is working great and I hope it will work for you as well Lets work together @ , come join us!is how hackers start their afternoons. We’re a part of the family. We are now and happy to opportunities.
To learn more, , , or simply,
If you enjoyed this story, we recommend reading our and trending tech stories. Until next time, don’t take the realities of the world for granted!