-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: improve useClickAway() hook #394
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { RefObject, useEffect } from 'react'; | ||
import { RefObject, useEffect, useRef } from 'react'; | ||
import { off, on } from './util'; | ||
|
||
const defaultEvents = ['mousedown', 'touchstart']; | ||
|
@@ -8,10 +8,14 @@ const useClickAway = ( | |
onClickAway: (event: KeyboardEvent) => void, | ||
events: string[] = defaultEvents | ||
) => { | ||
const savedCallback = useRef(onClickAway); | ||
useEffect(() => { | ||
savedCallback.current = onClickAway; | ||
}, [onClickAway]); | ||
useEffect(() => { | ||
const handler = event => { | ||
const { current: el } = ref; | ||
el && !el.contains(event.target) && onClickAway(event); | ||
el && !el.contains(event.target) && savedCallback.current(event); | ||
}; | ||
for (const eventName of events) { | ||
on(document, eventName, handler); | ||
|
@@ -21,7 +25,7 @@ const useClickAway = ( | |
off(document, eventName, handler); | ||
} | ||
}; | ||
}); | ||
}, [events, ref]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[...events, ref] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you're right 🤗 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, I'm trying to figure it out myself. And you approach is totally wrong, with spread operator in
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess you are right. I was thinking if useClickAway(ref, fn, ['mousedown', 'touchstart']) User needs to make sure |
||
}; | ||
|
||
export default useClickAway; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of saving the callback inside the
useClickAway
hook, isn't it better if consumer do it manually?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to write extra code on every
useClickAway
usageand this approach is less effective
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may I ask what is the purpos of using the useRef here ?
why not simply use the callback ?