Borislav Hadzhiev
Last updated: Apr 23, 2022
Photo from Unsplash
To get input values on form submit in React:
onSubmit
prop on the form element.handleSubmit
function.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;
We used the useState hook to track the values of the input fields.
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.
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:
ref
prop on each input fieldonSubmit
prop on the form element.ref.current.value
.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;
The example above uses uncontrolled input fields. Notice that the input fields
do not have an onChange
prop or value
set.
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.
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.
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.
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.
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;
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.
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.