R
Reactiflux

Deepak – 06-27 Sep 2

Deepak – 06-27 Sep 2

DDeepak9/2/2022
hi, I am using the following code to focus my auto search suggestion box. it works as expected. If user click outside the search result container it will hide the container. However it only target the document and not the entire screen for ex if i click on browser tabs or address bar it won't work
useEffect(() => {
//hide showSuggestion if the user clicked outside of the ref
const handleClickOutside = (event) => {
if (
searchSuggestionRef.current &&
!searchSuggestionRef.current.contains(event.target)
) {
setShowSuggestions(false);
}
};
document.addEventListener("mousedown", handleClickOutside);
return () => {
document.removeEventListener("mousedown", handleClickOutside);
};
}, [searchSuggestionRef]);
useEffect(() => {
//hide showSuggestion if the user clicked outside of the ref
const handleClickOutside = (event) => {
if (
searchSuggestionRef.current &&
!searchSuggestionRef.current.contains(event.target)
) {
setShowSuggestions(false);
}
};
document.addEventListener("mousedown", handleClickOutside);
return () => {
document.removeEventListener("mousedown", handleClickOutside);
};
}, [searchSuggestionRef]);
I have tried
window.addEventListener("mousedown", handleClickOutside);
window.addEventListener("mousedown", handleClickOutside);
but it doesn't work
UUUnknown User9/2/2022
Message Not Public
Sign In & Join Server To View
DDeepak9/2/2022
okay thanks
UUUnknown User9/2/2022
2 Messages Not Public
Sign In & Join Server To View

Looking for more? Join the community!

R
Reactiflux

Deepak – 06-27 Sep 2

Join Server