Skip to content

Instantly share code, notes, and snippets.

@BitOfUniverse
Created March 14, 2018 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BitOfUniverse/ecee2c3fa2ae41d0a31d7baec92f19d8 to your computer and use it in GitHub Desktop.
Save BitOfUniverse/ecee2c3fa2ae41d0a31d7baec92f19d8 to your computer and use it in GitHub Desktop.
Send onChange event in React
// in React after 15.6
const triggerInputChange = (node, value = '') => {
const inputTypes = [
window.HTMLInputElement,
window.HTMLSelectElement,
window.HTMLTextAreaElement
];
if (inputTypes.indexOf(node.__proto__.constructor) > -1) {
const setValue = Object.getOwnPropertyDescriptor(node.__proto__, 'value')
.set;
setValue.call(node, value);
const event = new Event('input', { bubbles: true });
node.dispatchEvent(event);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment