The KNVB
The KNVB

Reputation: 3844

React: Why the onChange event does not trigger?

I am building a date picker.

It works fine. Unfortunately, I have added the onChange handler to the input box, however, it does not be triggered.

I have referred to this post it told me that using the state variable can solve the problem.

However, I am using the useReducer instead of 'useState', is it why the onChange event does not be triggered?

Here is my code.

Upvotes: 0

Views: 1390

Answers (2)

The KNVB
The KNVB

Reputation: 3844

Finally, I use useEffect to monitor the change of context.displayDate to solve the problem.

useEffect(() => {
  console.log(contextValue.displayDate);      
}, [contextValue.displayDate]);

Upvotes: 1

Vuk
Vuk

Reputation: 873

I have tested your code and it seems if you update onChange to onBlur it works fine.

The documentation seems to suggest that onChange is only triggered for user changes to the value of the input field.

Example

 <input
   onBlur={e => handleChange(e)}
   onClick={showCalendar}
   type="text"
   value={contextValue.displayDate}
   readOnly
 />

Upvotes: 0

Related Questions