How to use event.target.value in React ?

Musa
3 min readNov 24, 2022
Photo by Juanjo Jaramillo on Unsplash

1 . State in React:

State in React is a way to manage dynamic data in your components. It allows you to track and update the values of properties that can change over time, such as user input or the results of an API call.

To use state, you first need to define it using the useState() hook. This hook takes an initial value as its argument and returns an array with two elements:

  • The current state value
  • A function to update the state value

For example:

JavaScript

const [count, setCount] = useState(0);

Here, count is the state variable and setCount is the function used to update it.

2. Using event.target.value:

The event.target.value property is used to access the value of an input element. It is commonly used in event handlers to capture user input and update the state of a component.

For example, consider an input field where the user can enter their name:

JavaScript

const [name, setName] = useState('');
<input type="text" onChange={(e) => setName(e.target.value)} />

--

--