对于点击事件
imgRef.current.dispatchEvent(new MouseEvent('click', { bubbles: true }));
React模拟点击事件是用的原生模拟, 一定要加上{ bubbles: true }, 冒泡
对于输入框React 16和React> = 15.6
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set;
nativeInputValueSetter.call(inputRef.current, '123');
inputRef.current.dispatchEvent(new Event('input', { bubbles: true}));
对于textarea元素,你应该使用prototype的HTMLTextAreaElement类
参考地址