Created
January 9, 2024 07:56
-
-
Save zhangshuo00/d9849d4f1b3f6066b3830a73dcc6158e to your computer and use it in GitHub Desktop.
Bind both click and doubleClick events on a component simultaneously, and no longer trigger click events when double clicking.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Button, message } from "antd" | |
const ClickComp = (props) => { | |
let clicks = 0 | |
let clickTimer = null | |
let clickspan = 300 | |
const handleClick = () => { | |
clicks++ | |
if (clicks === 1) { | |
clickTimer = setTimeout(() => { | |
clicks = 0 | |
message.info('click') | |
}, clickspan); | |
} | |
if (clicks === 2) { | |
clearTimeout(clickTimer) | |
clicks = 0 | |
} | |
} | |
const handleDbClick = () => { | |
message.info('dbclick') | |
} | |
return ( | |
<div> | |
<Button onClick={handleClick} onDoubleClick={handleDbClick}>click</Button> | |
</div> | |
) | |
} | |
export default ClickComp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment