I am currently working on a Youtube tutorial of react todo list by web dev simplifed
link
source codes
In the NewToForm.jsx form, there is a form with onSubmit handler
<form onSubmit={handleSubmit} className="new-item-form">
<div className="form-row">
<label htmlFor="item">New Item</label>
<input
value={newItem}
onChange={e => setNewItem(e.target.value)}
type="text"
id="item"
/>
</div>
<button className="btn">Add</button>
</form>
function handleSubmit(e) {
e.preventDefault()
if (newItem === "") return
onSubmit(newItem)
setNewItem("")
}
In the code, the value of onSubmit equals handleSubmit, which is a function that changes value of some states.
But as I know, an arrow function is needed to wrap the event handler, otherwise react keeps re-render for infinite number of times.
Why it is necessary to use Arrow functions with React Event handler | by kirill ibrahim | Dev Genius
So I want to ask why the event handler of submit is different from those of click and change? Is there any special about the handleSubmit function? Thank you!