首页 > 解决方案 > 如何用 React js 模拟长按?

问题描述

我想用点击事件触发长按事件。react js有什么办法吗?

与此相近的是 jQuery trigger() 函数。但我想要触发器(“longPress”)之类的东西,或者用左键点击打开右键菜单。两者都提到(长按触发器/打开右键菜单)对我来说是理想的

标签: javascriptreactjstouch-event

解决方案


像这样的东西怎么样:

const myComponent = () => {

    let clickHoldTimer = null;

    const handleMouseDown = () => {
        clickHoldTimer = setTimeout(() => {
            //Action to be performed after holding down mouse
        }, 1000); //Change 1000 to number of milliseconds required for mouse hold
    }

    const handleMouseUp = () => {
        clearTimeout(clickHoldTimer);
    }

    return (
        <div onMouseDown={handleMouseDown} onMouseUp={handleMouseUp} />
    )

}

推荐阅读