Skip to content

Instantly share code, notes, and snippets.

@zhangshuo00
Created January 9, 2024 07:56
Show Gist options
  • Save zhangshuo00/d9849d4f1b3f6066b3830a73dcc6158e to your computer and use it in GitHub Desktop.
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.
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