Skip to content

Commit

Permalink
fix: useLongPress onClick can't get the latest state (alibaba#1426)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperHuangXu authored Jan 4, 2022
1 parent 1a67662 commit 2ffa9b1
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/hooks/src/useLongPress/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function useLongPress(
{ delay = 300, onClick }: Options = {},
) {
const onLongPressRef = useLatest(onLongPress);
const onClickRef = useLatest(onClick);

const timerRef = useRef<ReturnType<typeof setTimeout>>();
const isTriggeredRef = useRef(false);
Expand All @@ -44,8 +45,8 @@ function useLongPress(
if (timerRef.current) {
clearTimeout(timerRef.current);
}
if (shouldTriggerClick && !isTriggeredRef.current && onClick) {
onClick(event);
if (shouldTriggerClick && !isTriggeredRef.current && onClickRef.current) {
onClickRef.current(event);
}
isTriggeredRef.current = false;
};
Expand Down

0 comments on commit 2ffa9b1

Please sign in to comment.