我熟悉selectstart
HTML中事件的用法。我onSelectStart
在JSX中使用。我希望它可以连接到onselectstart
。但是,它发出警告说它是未知的。
Warning: Unknown event handler property `onSelectStart`. It will be ignored.
示例代码:
function Example() {
return (
<div className="Example">
<h2 onSelectStart={() => console.log("Selection started")}>Click</h2>
</div>
);
}
我已经尝试了react/[email protected]
和[email protected]
。
有替代品onSelectStart
吗?如果是,该如何执行?
看来反应似乎不支持https://reactjs.org/docs/events.html#selection-events。即使在MDN文档上,这似乎也不是标准化事件:https : //developer.mozilla.org/en-US/docs/Web/API/Document/selectstart_event#Specifications,因此我会质疑其用法的有效性。
这是使用引用的解决方法:
https://codesandbox.io/s/fancy-wave-sotun
ref={el => {
el &&
el.addEventListener("selectstart", () => {
console.log("Selection started");
});
}}
只是不要忘记在卸载组件时删除事件侦听器。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句