{"id":351388,"date":"2024-05-20T12:45:29","date_gmt":"2024-05-20T12:45:29","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=351388"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=351388","title":{"rendered":"<span>React Custom Hook: useLongPress<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>In this article series, we embark on a journey through the realm of custom React hooks, discovering their immense potential for elevating your development projects. Our focus today is on the &#171;useLongPress&#187; hook, one of the many carefully crafted hooks available in the collection of React custom hooks.<\/p>\n<p>Github:\u00a0<a href=\"https:\/\/github.com\/sergeyleschev\/react-custom-hooks\" rel=\"noopener noreferrer nofollow\"><u>https:\/\/github.com\/sergeyleschev\/react-custom-hooks<\/u><\/a><\/p>\n<pre><code class=\"javascript\">import useEventListener from \"..\/useEventListener\/useEventListener\" import useTimeout from \"..\/useTimeout\/useTimeout\" import useEffectOnce from \"..\/useEffectOnce\/useEffectOnce\"  export default function useLongPress(ref, cb, { delay = 250 } = {}) {     const { reset, clear } = useTimeout(cb, delay)     useEffectOnce(clear)     useEventListener(\"mousedown\", reset, ref.current)     useEventListener(\"touchstart\", reset, ref.current)     useEventListener(\"mouseup\", clear, ref.current)     useEventListener(\"mouseleave\", clear, ref.current)     useEventListener(\"touchend\", clear, ref.current) }<\/code><\/pre>\n<p>One of the key advantages of useLongPress is its simplicity. By utilizing this hook, developers can easily define a long-press action on any element in their React application. With just a few lines of code, the hook takes care of handling the intricacies of tracking the long-press duration and triggering the associated callback function.<\/p>\n<p>The <strong>useLongPress<\/strong> hook offers flexibility through customizable options. Developers can specify the desired delay for a long press, allowing them to fine-tune the duration required for an action to be triggered. Additionally, the hook intelligently integrates with other custom hooks like useTimeout, useEventListener, and useEffectOnce, enhancing code reusability and maintainability.<\/p>\n<pre><code class=\"javascript\">import { useRef } from \"react\" import useLongPress from \".\/useLongPress\"  export default function LongPressComponent() {     const elementRef = useRef()     useLongPress(elementRef, () => alert(\"Long Press\"))     return (         &lt;div             ref={elementRef}             style={{                 backgroundColor: \"red\",                 width: \"100px\",                 height: \"100px\",                 position: \"absolute\",                 top: \"calc(50% - 50px)\",                 left: \"calc(50% - 50px)\",             }}         \/>     ) }<\/code><\/pre>\n<p>The applications for <em>useLongPress<\/em> are wide-ranging. Whether you&#8217;re developing a touch-sensitive UI, implementing context menus, or creating custom gestures, this hook proves to be a valuable tool. From mobile applications to complex web interfaces, useLongPress provides an elegant solution for incorporating long-press interactions that elevate user engagement and improve overall usability.<\/p>\n<p>Full Version | React Custom Hooks: <a href=\"https:\/\/habr.com\/en\/articles\/746760\/\" rel=\"noopener noreferrer nofollow\">https:\/\/habr.com\/en\/articles\/746760\/<\/a><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/752804\/\"> https:\/\/habr.com\/ru\/articles\/752804\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>In this article series, we embark on a journey through the realm of custom React hooks, discovering their immense potential for elevating your development projects. Our focus today is on the &#171;useLongPress&#187; hook, one of the many carefully crafted hooks available in the collection of React custom hooks.<\/p>\n<p>Github:\u00a0<a href=\"https:\/\/github.com\/sergeyleschev\/react-custom-hooks\" rel=\"noopener noreferrer nofollow\"><u>https:\/\/github.com\/sergeyleschev\/react-custom-hooks<\/u><\/a><\/p>\n<pre><code class=\"javascript\">import useEventListener from \"..\/useEventListener\/useEventListener\" import useTimeout from \"..\/useTimeout\/useTimeout\" import useEffectOnce from \"..\/useEffectOnce\/useEffectOnce\"  export default function useLongPress(ref, cb, { delay = 250 } = {}) {     const { reset, clear } = useTimeout(cb, delay)     useEffectOnce(clear)     useEventListener(\"mousedown\", reset, ref.current)     useEventListener(\"touchstart\", reset, ref.current)     useEventListener(\"mouseup\", clear, ref.current)     useEventListener(\"mouseleave\", clear, ref.current)     useEventListener(\"touchend\", clear, ref.current) }<\/code><\/pre>\n<p>One of the key advantages of useLongPress is its simplicity. By utilizing this hook, developers can easily define a long-press action on any element in their React application. With just a few lines of code, the hook takes care of handling the intricacies of tracking the long-press duration and triggering the associated callback function.<\/p>\n<p>The <strong>useLongPress<\/strong> hook offers flexibility through customizable options. Developers can specify the desired delay for a long press, allowing them to fine-tune the duration required for an action to be triggered. Additionally, the hook intelligently integrates with other custom hooks like useTimeout, useEventListener, and useEffectOnce, enhancing code reusability and maintainability.<\/p>\n<pre><code class=\"javascript\">import { useRef } from \"react\" import useLongPress from \".\/useLongPress\"  export default function LongPressComponent() {     const elementRef = useRef()     useLongPress(elementRef, () => alert(\"Long Press\"))     return (         &lt;div             ref={elementRef}             style={{                 backgroundColor: \"red\",                 width: \"100px\",                 height: \"100px\",                 position: \"absolute\",                 top: \"calc(50% - 50px)\",                 left: \"calc(50% - 50px)\",             }}         \/>     ) }<\/code><\/pre>\n<p>The applications for <em>useLongPress<\/em> are wide-ranging. Whether you&#8217;re developing a touch-sensitive UI, implementing context menus, or creating custom gestures, this hook proves to be a valuable tool. From mobile applications to complex web interfaces, useLongPress provides an elegant solution for incorporating long-press interactions that elevate user engagement and improve overall usability.<\/p>\n<p>Full Version | React Custom Hooks: <a href=\"https:\/\/habr.com\/en\/articles\/746760\/\" rel=\"noopener noreferrer nofollow\">https:\/\/habr.com\/en\/articles\/746760\/<\/a><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/752804\/\"> https:\/\/habr.com\/ru\/articles\/752804\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-351388","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/351388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=351388"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/351388\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=351388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=351388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=351388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}