Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

onclick hold react

import { useState, useEffect, useCallback } from 'react';

export default function useLongPress(callback = () => {}, ms = 300) {
  const [startLongPress, setStartLongPress] = useState(false);

  useEffect(() => {
    let timerId;
    if (startLongPress) {
      timerId = setTimeout(callback, ms);
    } else {
      clearTimeout(timerId);
    }

    return () => {
      clearTimeout(timerId);
    };
  }, [callback, ms, startLongPress]);

  const start = useCallback(() => {
    setStartLongPress(true);
  }, []);
  const stop = useCallback(() => {
    setStartLongPress(false);
  }, []);

  return {
    onMouseDown: start,
    onMouseUp: stop,
    onMouseLeave: stop,
    onTouchStart: start,
    onTouchEnd: stop,
  };
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js onclick 
Javascript :: Content security policy (csp) javascript 
Javascript :: update data in json using javascript 
Javascript :: react native swiper 
Javascript :: install express generator 
Javascript :: ticking clock react js 
Javascript :: nginx get request method 
Javascript :: assign random colors react chartjs 
Javascript :: createelement add class 
Javascript :: react 
Javascript :: javascript Program with a Promise 
Javascript :: how to resize image in react js 
Javascript :: detect if overflow javascript 
Javascript :: import and export type in js 
Javascript :: how to practice javascript 
Javascript :: javascript sort an array 
Javascript :: js get datatable attr value on click 
Javascript :: how to get property names from object using map method react 
Javascript :: how to append object in array javascript 
Javascript :: this.setstate is not a function in react native 
Javascript :: react functional components shortcut in webstorm 
Javascript :: momentjs get calendar week 
Javascript :: js how to see console day tomorrow 
Javascript :: chrome.storage.local delete 
Javascript :: remove all parameters from url javascript 
Javascript :: Return the highest number in Arrays in JavaScript 
Javascript :: Generate a Random Integer 
Javascript :: line break in js 
Javascript :: multi dimensional array javascript 
Javascript :: react native image with header and body 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =