Several types of suicide by form in React

Tram Ho

With the current requirements of rapidly changing requirements, the design of suitable architecture architecture for the project is extremely careful consideration. Form design is an integral part of the frontend, and some of the following technical decisions will help you get unforgettable experiences.

Early abstraction (early design is usually design failure)

The fastest thing we can do to refactor that form is to gather the same UI components into a component to reuse and feel proud of ourselves.

For example:

Until the new spec comes, with all sorts of things, such as:

  • The input for the customer name in RegisterForm is manual input, but in UpdateForm, it is to fill from API => simple, add conditions to check whether the data from props is available, if not, then display it empty.

  • Input the products purchased in the RegisterForm form is allowed to enter, while in UpdateForm it is not => in it is still simple, pass the form type into the CustomerContact props, to know where it is rendered from: v

  • RegisterForm calls api abc, xyz, and UpdateForm does not => ez !, n if if condition
  • RegisterForm will redirect to a confirmation page, but UpdateForm will not => ez! Na if else condition
  • RegisterForm displays the modal for the authentication code, while UpdateForm only displays the form asking Yes / No, are u sure, etc. => ez! Na if else condition
  • ….

Gradually, we realized, more and more the components that we wrote “to reuse” bulged with the conditions at the top. And adding features to the form becomes immersed in conditional statements.

So what is the problem here? Is to write a component to reuse the UI, regardless of whether its state is really related to each other.

Monolithic state (state clumping)

In addition to the devil’s behavior of inserting the state of the form into Redux, we can also dance in a similar magic way, such as: letting the state of the global form focus on it, and then using the Context API to access it. For not to chisel props.

Increasingly, the editing of the form became more and more confusing, and we gradually felt that the propagation of props from father to son / grandchild / great-grandchild / chit, it was much more relaxed.

Versalite input (universal input)

I wish I could copy and paste the source code here … but anyway, imagine, a versatile input is:

  • it can call api to get data for itself => loose, and we connect redux and dispatch actions like a real container.
  • it can format the data itself according to the 69 way the component uses it, it only needs to pass flags or a specific data type => yeah, with a condition of 69 or less conditions, you will see hook types that weird useEffect.
  • It is directly connected to the global state, and the display changes according to the state changes, everything we do is update the state and all related inputs will change to => sure yeah and the same input, same state, in somewhere else, another display, we have to update the state and when we leave, we have to return the state to the way it was.

In general, the universal input always chokes inside, initially it will be convenient until it becomes a lump of debt that we have to maintain.

Conclusion

In the form, the state is the source of all suffering, and the above are some basic paths to experience it. It’s not difficult to dodge, it’s hard because I want to avoid it! Anyway … here are a few things to keep in mind:

  1. Duplicate code is acceptable, so use it because code duplication is easy to add / remove / edit / delete.
  2. Wrong abstraction is to keep seeing that it is the same as gathering it into reuse, note that we only consider it “the same” when its state / operation flow is the same.
  3. Storing the state of a form in any global form is never as delicious as a clean, clean genealogy tree.
  4. Components that have reuse (such as TextInput, PhoneInput inputs) must not have it related to the state of the form at all, it should only care about its local state only.
Share the news now

Source : Viblo