How to get the value of an input element in React

Given a form, how do you get the value of one of the form fields?

A common situation involves having a form and you want to get the value of one of the form fields, for example, when the user clicks a button.

How can you do this?

Using hooks, you can create a variable for each input field and thenonChangeIf you call the "Set" function for this variable.

This is an example:

const [title, setTitle] = useState('')

On the input field of JSX:

<input onChange={event => setTitle(event.target.value)} />

In this way, when you perform the Submit event of the form in the event handler, or anywhere you need it, you can get the value of the field from the form.titlevalue.

Download mine for freeResponse Handbook


More response tutorials: