tip

hey so uhm if you want you should microwave your glazed donuts because it tastes dope as peepee. “tip” is published by ✄.

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Writing Forms in React

HTML forms are hard…
Developing forms in react could be even harder:

We have to render each input control using its recent value, but still to maintain the overall form state so we’ll be able to submit its values in a single transaction.

Take a look over the following naive implementation:
Fill both inputs and hit ‘submit’.
Now - check the ‘Use default values’ checkbox and try again.

What went wrong?
By initiating the input with props value, user changes stop to take affect.

However, using this pattern enforce us to implement a dedicated from-container component with its own predefined state each time.
Over and over again…

I’d like to suggest a new approach, using only react native API.
What if — we’ll be able to use controlled-components strength but also preserve common reusable functionality?

In order to do so, we’ll have to agree over a common field ← → form interface:

The Filed component is a general controlled-component, it has an initial value defined by its props.value, and a internal state.newValue been updated by user interaction.

Now, let’s have a look over the Form component implementation:

Let take a closer look over the handleChange() event handler:

The form component updates its inner fields state upon every change.
In this way — when the user submit the form, the fields set is already up-to-date with each field latest value.
All there left to do, is to construct a suitable dictionary/object, and send it away!

Add a comment

Related posts:

Why call a professional Dryer vent cleaning company?

One of the most common causes of dryer fires is dirty dryer vents. By calling a professional Dryer vent cleaning company in Brandon, you can ensure that these dangerous blazes are eliminated from…

MADworld Sold Out 400 MADminds in Under 2 Minutes on the ApolloX NFT Launchpad from the PAID Network

MADworld launched the first round of its MADminds PFP (profile picture) NFTs on PAID Network’s NFT launchpad, ApolloX on Saturday November 20, 2021. MADworld allocated 400 Virtual Genius MADminds out…