Handling events is the key to building dynamic apps with React. Understanding event.target.value is the key to managing user inputs in React.

event.target.value in React

Before we get to event.target.value, let’s understand what event is.

What is event?

When event happens in React, usually we execute some function in response to that event, also called an event handler.

The function always receives one argument – SyntheticEvent – an object that stores useful information about the event that just happened. The object is usually referred to e or event as an argument in function definition.

What is event.target?

Now that we know what event is, let’s talk its target property.

event.target refers to the input element where the event happened – if user clicked the button, event.target would be a button. If the user entered (or deleted) a character from the field, event.target would refer to the input field, and so on.

What is event.target.value in React?

As we established, event.target object represents an input element where change happened. Its value property stores element’s current value. If you’re working with an input field, event.target.value would return what’s currently entered into the field.

Let’s look at an example:

function App() {
  return (
    <div>
      <input type="text" onChange={(e) => console.log(e.target.value)} />
    </div>
  );
}

event.target.value very useful for capturing users inputs and making sure your app stays up to date to users’ actions.

It can be essential for implementing dynamic features like conditional rendering, conditional styles, and so on.

event.target.value not working in React

If event.target.value is not working, make sure that:

  1. Event handler is set on the right element. Not the container, but the input field itself.
  2. You are using the right type of event. For example, button inputs respond to click events, not change events. Buttons also do not have an inherent value, unlike input fields.
  3. Double-check the name of argument standing in for SyntheticEvent. Usually it’s event or e. In case of event,you should be able to access event.target.value. In case the argument is e, you should access input value on e.target.value.