Skip to content

Commit 8f0f188

Browse files
Merge pull request #198 from HermanNygaard/main
Fix stale closures in ref callbacks
2 parents 475d373 + 6892fb7 commit 8f0f188

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ export function useClickAway(cb) {
9898
const ref = React.useRef(null);
9999
const refCb = React.useRef(cb);
100100

101+
React.useLayoutEffect(() => {
102+
refCb.current = cb;
103+
});
104+
101105
React.useEffect(() => {
102106
const handler = (e) => {
103107
const element = ref.current;
@@ -591,6 +595,10 @@ export function useLongPress(
591595
const timerId = React.useRef();
592596
const cbRef = React.useRef(callback);
593597

598+
React.useLayoutEffect(() => {
599+
cbRef.current = callback;
600+
});
601+
594602
const start = React.useCallback(
595603
() => (event) => {
596604
if (isPressed.current) return;

0 commit comments

Comments
 (0)