Neat and refreshing approach! :)
Please confirm if the following makes sense:
In the App component, a new instance of the callback function is created on each render:
const modalRef = useOnClickOutsideRef(() => setIsOpen(false))
In the useOnClickOutsideRef hook, the passed callback is used as a dependency of the effect:
useOnClickOutsideRef
}, [callback]) <-- return elementRef }
Thus, the window.click is unmounted and mounted again each time the component that calls useOnClickOutsideRef is rendered.
window.click
Providing the useMemo-wrapped callback ensures the same function is passed down the hook:
useMemo
// App const modalRef = useOnClickOutsideRef(useMemo(() => setIsOpen(false), []));
Yes, you are right. I missed that.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Neat and refreshing approach! :)
Please confirm if the following makes sense:
In the App component, a new instance of the callback function is created on each render:
In the
useOnClickOutsideRef
hook, the passed callback is used as a dependency of the effect:Thus, the
window.click
is unmounted and mounted again each time the component that callsuseOnClickOutsideRef
is rendered.Providing the
useMemo
-wrapped callback ensures the same function is passed down the hook:Yes, you are right. I missed that.