Get input values on Form submit in React

avatar

Borislav Hadzhiev

Last updated: Apr 23, 2022

banner

Photo from Unsplash

Get input values on Form submit in React #

To get input values on form submit in React:

  1. Store the values of the input fields in state variables.
  2. Set the onSubmit prop on the form element.
  3. Access the values of the input fields in your handleSubmit function.
App.js
import {useState} from 'react'; const App = () => { const [firstName, setFirstName] = useState(''); const [lastName, setLastName] = useState(''); const handleSubmit = event => { console.log('handleSubmit ran'); event.preventDefault(); // 👈️ prevent page refresh // 👇️ access input values here console.log('firstName 👉️', firstName); console.log('lastName 👉️', lastName); // 👇️ clear all input values in the form setFirstName(''); setLastName(''); }; return ( <div> <form onSubmit={handleSubmit}> <input id="first_name" name="first_name" type="text" onChange={event => setFirstName(event.target.value)} value={firstName} /> <input id="last_name" name="last_name" type="text" value={lastName} onChange={event => setLastName(event.target.value)} /> <button type="submit">Submit form</button> </form> </div> ); }; export default App;

get form input value on submit

We used the useState hook to track the values of the input fields.

We set the onChange prop on the fields, so when their values change, we update the corresponding state variables.

The button element in the form has type of submit, so every time it is clicked, the submit event is fired on the form.

We used the event.preventDefault() method in the handleSubmit function to prevent the page from refreshing when the form is submitted.

To get the input values on form submit, we simply access the state variables.

You can set the state variables to empty strings if you want to clear the values of the fields after the form has been submitted.

Alternatively, you can use uncontrolled input fields.

To get input values on form submit in React:

  1. Set the ref prop on each input field
  2. Set the onSubmit prop on the form element.
  3. Access the input values on the ref objects, e.g. ref.current.value.
App.js
import {useRef} from 'react'; const App = () => { const firstRef = useRef(null); const lastRef = useRef(null); const handleSubmit = event => { console.log('handleSubmit ran'); event.preventDefault(); // 👈️ prevent page refresh // 👇️ access input values here console.log('first 👉️', firstRef.current.value); console.log('last 👉️', lastRef.current.value); // 👇️ clear all input values in the form event.target.reset(); }; return ( <div> <form onSubmit={handleSubmit}> <input ref={firstRef} id="first_name" name="first_name" type="text" /> <input ref={lastRef} id="last_name" name="last_name" type="text" /> <button type="submit">Submit form</button> </form> </div> ); }; export default App;

get form input value on submit ref

The example above uses uncontrolled input fields. Notice that the input fields do not have an onChange prop or value set.

You can pass an initial value to an uncontrolled input with the defaultValue prop. However, this is not necessary and you can omit the prop if you don't want to set an initial value.

When using uncontrolled input fields, we access the input using a ref.

The useRef() hook can be passed an initial value as an argument. The hook returns a mutable ref object whose .current property is initialized to the passed argument.

Notice that we have to access the current property on the ref object to get access to the input element on which we set the ref prop.

When we pass a ref prop to an element, e.g. <input ref={myRef} />, React sets the .current property of the ref object to the corresponding DOM node.

The useRef hook creates a plain JavaScript object, but gives you the same ref object on every render. In other words, it's pretty much a memoized object value with a .current property.

It should be noted that when you change the value of the current property of the ref, no re-renders are caused.

Every time the user submits the form in the example, the values of the uncontrolled inputs get logged.

You shouldn't set the value prop on an uncontrolled input (an input field that doesn't have an onChange handler) because that would make the input field immutable and you wouldn't be able to type in it.

You can use the reset() method if you want to clear the values of the uncontrolled inputs after the form has been submitted.

The reset() method restores a form element's default values.

Regardless of how many uncontrolled input fields your form has, a single call to the reset() method clears all of them.

An alternative way to get the values of the input fields when the form is submitted, is to access the form elements using their name prop.

App.js
const App = () => { const handleSubmit = event => { console.log('handleSubmit ran'); event.preventDefault(); // 👇️ access input values using name prop console.log('first 👉️', event.target.first_name.value); console.log('second 👉️', event.target.last_name.value); // 👇️ clear all input values in the form event.target.reset(); }; return ( <div> <form onSubmit={handleSubmit}> <input id="first_name" name="first_name" type="text" /> <input id="last_name" name="last_name" type="text" /> <button type="submit">Submit form</button> </form> </div> ); }; export default App;

get form input value on submit ref

We used the name prop to access the values of the input fields when the form is submitted.

The target property on the event object refers to the form element.

You won't see this approach used often and it's mostly a quick and dirty solution if you don't want to store the values of the input fields in the state or use ref objects.

The most commonly used approach is to store input values in state variables. The ability to access state variables from anywhere allows for highly customizable forms.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.