1

The problem is that I'm unable to manually trigger onChange callback on input component in React. I have a custom 'file picker' which changes the input's value of new selected file path. I want to manually dispatch a 'change'/'input' event for this input.

Example case:

<input onChange={(e) => console.log(e.target.value)} />
const inputRef = /* ref of input above */

/* .. */
const newValue = 'path/test.ext';
inputRef.current.value = newValue;
// dispatch change event for inputRef.current to console log new value

I'd like to point out that I've already researched the subject. The newest solution I've could find is: https://stackoverflow.com/a/71340077/14175627

Sadly, this solution doesn't work anymore and I'm unable to find working one.

kuba
  • 141
  • 5
  • Does this answer your question? [What is the best way to trigger onchange event in react js](https://stackoverflow.com/questions/23892547/what-is-the-best-way-to-trigger-onchange-event-in-react-js) – Randy Casburn Feb 03 '23 at 15:59
  • See the accepted answer to the duplicate: https://stackoverflow.com/a/46012210/9078341 – Randy Casburn Feb 03 '23 at 16:00
  • Alternatively you can use [react-trigger-change](https://github.com/vitalyq/react-trigger-change) – Randy Casburn Feb 03 '23 at 16:01
  • The solution you gave I mentioned in the post. It doesn't work anymore in newest React version. – kuba Feb 03 '23 at 16:22

0 Answers0