REACT 2, Input props and onChange(), React form onSubmit() and formData, input multiple attribute an

React Input Props and onChange() event handler

ReactJs input props allow us to have controlled inputs, in which we set an input's value using useState() and can edit it using an onChange() event handler.

We re-set the value of the useState() based on the input current e.target.value (target being a property of the event interface, referencing the input).

const [costo, setCosto] = useState(0)

<div>
  <label htmlFor="cash" className="form-label">How much spending?</label>
  <input id="cash" type="range" className="form-range" value={costo} 
         onChange={(e)=> setCosto(e.target.value)} name="costo" />
  <p className="text-center">Price being: {costo}$</p>
</div>
Changing input value gets rendered with useState()
chevron-rightuseState() with number inputhashtag

When updating an integer useState() outside the input we need to convert it with Number(), to maintain the single useState().

Input number with Onchange

Text and radio/checkbox inputs use defaultValue/checked for the default value prop, if the checkbox doesn't have a value prop on submit its value will be "on".

Defaultchecker() value set

We use the useId() React hook to avoid conflicts when re-rendering multiple labels/inputs.

defaultValue text and checked checkbox

We use the max/min input prop to set its submit conditions, the event handler onInvalid() is triggered when the submit fails.

The error messae on failed submit is included

React Form onSubmit() event handler and FormData()

A type="submit" button will trigger the form onSubmit() event handler.

We then use new FormData(form, submitter) interface to render the e.target HTML form from onSubmit(), as a name/value iterable.

We finally use the Object.fromEntries(iterable) static method to convert the FormData into an Object.

form onSubmit() and formData object

We use FormData methods to edit the form object.

We can use append(key, value) and set(key, value) to add a new key/value pair, delete(key) to remove one, get(key) to return the first value associated with the key, and has(key) which returns true/false depending if the key is present.

We can use the entries(), keys(), and values() methods to return a key/values, keys, and values iterable.

Input file image render and multiple input attribute

On type="file" <input> we use the accept property which sets the MIME file types we can submit. We can use accept= "audio/*" and "image/*" and "video/*" in case we want to accept any file type.

form-control file input and image submit

Its returned formdata will be an object of image properties, the lastModified (the UNIX epoc number) in milliseconds, its size in bytes, the file's name, and its MIME type.

chevron-rightInput image file formdatahashtag

The accept property isn't enough to filter the input files so we use a server-side function.

The Byte is the basic unit of file storage, based on the binary system the Kilobyte will be 1024 bytes (2^10) while the Megabyte is 1024^2.

To render the image src we need to URL.createObjectURL() the entire image object.

Image formdata properties

The multiple input attribute allows submitting of multiple values in a single input.

If used on a type="text" input, the values need to be separated with a comma (,), any space will be cut in the formdata value.

If added to <select> it will expand its size, we will use ctrl + click to multiple select on pc (while we get a pop-up on mobile)

chevron-rightMultiple attribute <input> and Javascript expression renderhashtag

We can limit the size of an <select> after we add multiple.

We will render the javascript expressions after the formdata is submitted.

multiple input submit and render
chevron-rightRendering formdata entries for multiple <input>hashtag

The formdata object from a multiple <input> will only show the LAST value of each name/key but the formdata iterable will keep track of every multiple input value.

We use an useState([]) to render an array of React node elements as a javascript expression {}.

We can't use push() for the useState() because it returns the array's length, not the edited array, while concat() returns the "new" array.

multiple input formdata object

Last updated