首先,这不是特定于react的问题。
如何避免这个错误?
{ cancelable: true }
在这种情况下,该标志似乎阻止了 Firefox 重新加载页面。
this.form.dispatchEvent(new Event('submit', { cancelable: true }))
这是一个完整而简单的示例:
<!Doctype html>
<html>
<head>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
const form = document.querySelector('#testform');
const button = form.querySelector('button');
form.onsubmit = function (e) {
e.preventDefault();
e.stopPropagation();
console.log('submitting the form ...');
// return true;
return false;
};
// https://developer.mozilla.org/en-US/docs/Web/API/Event/Event
// cancelable: (optional) a Boolean indicating whether the event can be cancelled. The default is false.
const submitEvent = new Event('submit', {
cancelable: true
});
// https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted
// The isTrusted read-only property of the Event interface is a boolean that is true when the event was generated by a user action, and false when the event was created or modified by a script or dispatched via dispatchEvent.
console.log('is event trusted', submitEvent.isTrusted) // false
console.log('is event cancelable', submitEvent.cancelable) // true
button.onclick = function () {
console.log('button clicked');
const cancelled = form.dispatchEvent(submitEvent);
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent
// The return value is false if event is cancelable and at least one of the event handlers which handled this event called Event.preventDefault(). Otherwise it returns true.
console.log('is cancelled', !cancelled);
}
});
</script>
</head>
<body>
<form id="testform">
<input type="hidden" name="a" value="b">
<p>Hit the button a couple times, the page should not refresh.</p>
<button type="button">click</button>
</form>
</body>
</html>
为什么会发生这种情况:
https://www.chromestatus.com/features/5718803933560832
根据 UI 事件规范,不受信任的事件(即由 JavaScript 创建的事件)不应调用默认操作。
如果您将可取消标志更改为 false 并在 Chrome 中尝试测试,它仍然可以工作,因为(据我所知)它仍然是一个不受信任的事件(不是由用户与 UI 的直接交互创建的)并且 Chrome 不会默认运行不受信任事件的处理程序。
但是,我不确定为什么 Firefox 仍然为不受信任的事件运行默认处理程序。