如何在React / JSX中触发onSelectStart?

阿比纳夫·基纳吉

我熟悉selectstartHTML事件的用法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]

编辑loving-proskuriakova-ute7j

有替代品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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章