Skip to content
🐁 A simple React hook for differentiating single and double clicks on the same component.
JavaScript
Branch: master
Clone or download
Latest commit 52274d6 Sep 11, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.vscode 🚀 Initial commit Aug 5, 2019
src
.babelrc.js 🚀 Initial commit Aug 5, 2019
.editorconfig 🚀 Initial commit Aug 5, 2019
.eslintignore 🚀 Initial commit Aug 5, 2019
.eslintrc.js Add test suite with Jest and @testing-library Aug 6, 2019
.gitignore Add test suite with Jest and @testing-library Aug 6, 2019
.prettierignore 🚀 Initial commit Aug 5, 2019
.prettierrc.js
.travis.yml Add travis-ci config Aug 6, 2019
CHANGELOG.md Update CHANGELOG.md Aug 6, 2019
LICENSE
README.md Update README.md Sep 11, 2019
jest.config.js Add test suite with Jest and @testing-library Aug 6, 2019
package.json
rollup.config.js
yarn.lock upgrade deps Aug 27, 2019

README.md

use-double-click

npm NPM npm bundle size PRs Welcome Codecov Travis (.org)

use-double-click is a simple React hook for differentiating single and double clicks on the same component.

Documentation https://timellenberger.com/libraries/use-double-click

Check out the demo on Codesandbox

What's wrong with onDoubleClick()?

When you double click on an element, onClick() fires twice alongside your single onDoubleClick() callback. This effect isn't desirable when a single click and a double click have different functions!

useDoubleClick() waits within a latency window after a click for a secondary click, and only after this period either the onSingleClick or onDoubleClick() callback will fire a single time.

Install

yarn add use-double-click

Usage

import { useRef } from 'react';
import useDoubleClick from 'use-double-click';

const Button = () => {
  const buttonRef = useRef();
  
  useDoubleClick({
    onSingleClick: e => {
      console.log(e, 'single click');
    },
    onDoubleClick: e => {
      console.log(e, 'double click');
    },
    ref: buttonRef,
    latency: 250
  });
  
  return <button ref={buttonRef}>Click Me</button>
}

Props

Prop Description
onSingleClick A callback function for single click events
onDoubleClick A callback function for double click events
ref Dom node to watch for double clicks
latency The amount of time (in milliseconds) to wait before differentiating a single from a double click

License

MIT © Tim Ellenberger

You can’t perform that action at this time.