Get the value of an Input field in React

avatar

Borislav Hadzhiev

Sat Apr 23 20223 min read

banner

Photo by Tim Marshall

Get the value of an Input field in React #

To get the value of an input field in React:

  1. Declare a state variable that tracks the value on the input field.
  2. Add an onChange prop to the input field.
  3. Use event.target.value to get the input field's value and update the state variable.
App.js
import {useState} from 'react'; const App = () => { const [message, setMessage] = useState(''); const handleChange = event => { setMessage(event.target.value); console.log('value is:', event.target.value); }; return ( <div> <input type="text" id="message" name="message" onChange={handleChange} value={message} /> <h2>Message: {message}</h2> </div> ); }; export default App;

get input value

We used the useState hook to track the value of the input field.

We set the onChange prop on the field, so every time its value changes, the handleChange function is invoked.

We can access the value of the input element as event.target.value in the handleChange function.

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

You can use the message state variable to access the value of the input field anywhere outside the handleChange function.

Alternatively, you can use an uncontrolled input field.

To get the value of an uncontrolled input field in React:

  1. Initialize a ref using the useRef hook.
  2. Set the ref on the input field.
  3. Access the value of the input field as ref.current.value.
App.js
import {useRef} from 'react'; const App = () => { const inputRef = useRef(null); function handleClick() { console.log(inputRef.current.value); } return ( <div> <input ref={inputRef} type="text" id="message" name="message" /> <button onClick={handleClick}>Log message</button> </div> ); }; export default App;

react get uncontrolled input value

The example above uses an uncontrolled input. Notice that the input field does 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 clicks on the button in the example, the value of the uncontrolled input gets 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.

Use the search field on my Home Page to filter through my more than 1,000 articles.