@@ -640,23 +640,18 @@ export function useLockBodyScroll() {
640640 } , [ ] ) ;
641641}
642642
643- export function useLongPress (
644- callback ,
645- { threshold = 400 , onStart, onFinish, onCancel } = { }
646- ) {
643+ export function useLongPress ( callback , options = { } ) {
644+ const { threshold = 400 , onStart, onFinish, onCancel } = options ;
647645 const isLongPressActive = React . useRef ( false ) ;
648646 const isPressed = React . useRef ( false ) ;
649647 const timerId = React . useRef ( ) ;
650- const cbRef = React . useRef ( callback ) ;
651648
652- React . useLayoutEffect ( ( ) => {
653- cbRef . current = callback ;
654- } ) ;
655-
656- const start = React . useCallback (
657- ( ) => ( event ) => {
658- if ( isPressed . current ) return ;
649+ return React . useMemo ( ( ) => {
650+ if ( typeof callback !== "function" ) {
651+ return { } ;
652+ }
659653
654+ const start = ( event ) => {
660655 if ( ! isMouseEvent ( event ) && ! isTouchEvent ( event ) ) return ;
661656
662657 if ( onStart ) {
@@ -665,15 +660,12 @@ export function useLongPress(
665660
666661 isPressed . current = true ;
667662 timerId . current = setTimeout ( ( ) => {
668- cbRef . current ( event ) ;
663+ callback ( event ) ;
669664 isLongPressActive . current = true ;
670665 } , threshold ) ;
671- } ,
672- [ onStart , threshold ]
673- ) ;
666+ } ;
674667
675- const cancel = React . useCallback (
676- ( ) => ( event ) => {
668+ const cancel = ( event ) => {
677669 if ( ! isMouseEvent ( event ) && ! isTouchEvent ( event ) ) return ;
678670
679671 if ( isLongPressActive . current ) {
@@ -692,31 +684,24 @@ export function useLongPress(
692684 if ( timerId . current ) {
693685 window . clearTimeout ( timerId . current ) ;
694686 }
695- } ,
696- [ onFinish , onCancel ]
697- ) ;
698-
699- return React . useMemo ( ( ) => {
700- if ( callback === null ) {
701- return { } ;
702- }
687+ } ;
703688
704689 const mouseHandlers = {
705- onMouseDown : start ( ) ,
706- onMouseUp : cancel ( ) ,
707- onMouseLeave : cancel ( ) ,
690+ onMouseDown : start ,
691+ onMouseUp : cancel ,
692+ onMouseLeave : cancel ,
708693 } ;
709694
710695 const touchHandlers = {
711- onTouchStart : start ( ) ,
712- onTouchEnd : cancel ( ) ,
696+ onTouchStart : start ,
697+ onTouchEnd : cancel ,
713698 } ;
714699
715700 return {
716701 ...mouseHandlers ,
717702 ...touchHandlers ,
718703 } ;
719- } , [ callback , cancel , start ] ) ;
704+ } , [ callback , threshold , onCancel , onFinish , onStart ] ) ;
720705}
721706
722707export function useMap ( initialState ) {
0 commit comments