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>


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".
//We use an useState() to set a defaultChecked(works for value too)
const [lenguage, setLenguage] = useState( "Hindi" );
<div className="form-check">
<input type="radio" name='language' defaultChecked={true} value="hindi"
onChange={(e)=> setLenguage(e.target.value) } />
<label >Hindi</label>
</div>
<div className="form-check">
<input type="radio" name='language' value="spanish"
onChange={(e)=> setLenguage(e.target.value) } />
<label>Spanish</label>
</div>

We use the useId() React hook to avoid conflicts when re-rendering multiple labels/inputs.
//We can add strings to the useId() to avoid callin it multiple times
const testo= useId()
<form className="row">
<div className="col-6">
<input id={testo} type="text" className="form-control"
defaultValue="any useState()" />
</div
<div className="col-6">
<div className="form-check">
<label htmlFor={testo + "check"} className="form-check-label">checked</label>
<input id={test + "check"} type="checkbox" checked={true}
value="any useState()" className="form-check-input" />
</div>
</div>
<div className="col-4">
<button className="btn btn-primary">Submit</button>
</div>
</form>

We use the max/min input prop to set its submit conditions, the event handler onInvalid() is triggered when the submit fails.
//For text input we use min/manLenght
<form>
<input type="text" className="form-control" maxLength="10" minLength="4"
onInvalid={()=> {console.log("riprova")}}/>
<div>
<label htmlFor={testo} className="form-label">Numeri</label>
<input id={testo} type="number" className="form-control" min="120" max="400" />
</div>
<button className="btn btn-primary">Submit</button>
</form>

React Form onSubmit() event handler and FormData()
A type="submit" button will trigger the form onSubmit() event handler.
//You can use type="reset" to reset the form or type="button" to not form submit
const [mino, setMino] = useState("")
<form onSubmit={invia}>
<input type="text" className="form-control" name="elemento"
value={mino} onChange={(e)=> setMino(e.target.value)} />
<input type="checkbox" name="checked" className="form-check-input" />
<button type="submit" className="btn btn-primary">Submit</button>
</form>
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.
//The keys/names are gonna be in alphabetical order
//If the checkbox is not selected its name won't appear in the formData
function manda(e){
e.preventDefault()
let forma= e.target
let formdata = new FormData(forma)
let dati = Object.fromEntries(formdata.entries())
console.log(dati)
}

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.
//Values will be converted in strings (like "true" and "96")
let form= e.target
let formdata= new FormData(form)
formdata.append("nuovo", 101)
formdata.set("vecchio", "penny")
console.log( formdata.get("nuovo") ) //"101"
formdata.delete("nuovo")
console.log( formdata.has("nuovo") ) //"false"
We can use the entries(), keys(), and values() methods to return a key/values, keys, and values iterable.
//In order to get the values we need to loop the iterable
for (const pair of formdata.entries()) {
console.log( pair ); //["checked", "on"], ["elemento","messo"]
}
formdata.values() //checked, elemento
formdata.keys() //on, messo
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.
//With multiple accept properties we use (,), we use the formdata for image src
<div className="col-3">
<form onSubmit={immagine} className="text-center">
<div className="mb-3">
<label htmlFor="nsa" className="form-label"></label>
<input id="nsa" type="file" className="form-control"
name="sauce" accept=".png, .webp"/>
</div>
<button type="submit" className="btn btn-primary">Submit</button>
</form>
</div>
<div className="col-3" >
<img src={vedere} className="img-fluid" />
<p> {digitale} </p>
</div>

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.

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)


Last updated
Was this helpful?