File size: 1,067 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# `useLongPress`
React sensor hook that fires a callback after long pressing.
## Usage
```jsx
import { useLongPress } from 'react-use';
const Demo = () => {
const onLongPress = () => {
console.log('calls callback after long pressing 300ms');
};
const defaultOptions = {
isPreventDefault: true,
delay: 300,
};
const longPressEvent = useLongPress(onLongPress, defaultOptions);
return <button {...longPressEvent}>useLongPress</button>;
};
```
## Reference
```ts
const {
onMouseDown,
onTouchStart,
onMouseUp,
onMouseLeave,
onTouchEnd
} = useLongPress(
callback: (e: TouchEvent | MouseEvent) => void,
options?: {
isPreventDefault?: true,
delay?: 300
}
)
```
- `callback` — callback function.
- `options?` — optional parameter.
- `isPreventDefault?` — whether to call `event.preventDefault()` of `touchend` event, for preventing ghost click on mobile devices in some cases, defaults to `true`.
- `delay?` — delay in milliseconds after which to calls provided callback, defaults to `300`.
|