Skip to content

Instantly share code, notes, and snippets.

@kwabe007
kwabe007 / dispatch-event-react.js
Created February 13, 2024 19:33
How to dispatch an event using pure js on an input element managed by React.
// All credits to @kentcdodds
// https://github.com/facebook/react/issues/10135#issuecomment-401496776
function setNativeValue(element, value) {
const { set: valueSetter } = Object.getOwnPropertyDescriptor(element, 'value') || {}
const prototype = Object.getPrototypeOf(element)
const { set: prototypeValueSetter } = Object.getOwnPropertyDescriptor(prototype, 'value') || {}
if (prototypeValueSetter && valueSetter !== prototypeValueSetter) {
prototypeValueSetter.call(element, value)