We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Thanks for the awesome hook!
Apologies if this is obvious but how would you remove the event listener? Let's say i'm attaching it based on the value of another variable..
if (editState === 'haschanges) { useEventListenter('beforeunload', handler); } else { // remove the listener }
The text was updated successfully, but these errors were encountered:
may be passing the condition to the hook for removing .
useEventListenter('beforeunload', handler, editState === 'haschanges');
or may be simply passing handler as null
useEventListenter('beforeunload', editState === 'haschanges' ? handler : null);
Sorry, something went wrong.
I think the cleanest way is for useEventListenter to return an object with a reference to the "cleanup" function, possibly called remove.
useEventListenter
remove
const cleanupRef = useRef(); ... useEffect(() => { ... cleanupRef.current = () => { element.removeEventListener(eventName, eventListener, opts); }; return cleanupRef.current; }, [eventName, element, capture, passive, once]); return { remove: cleanupRef }; };
I'm not sure about returning a useRef vs. its current value, but you get the idea.
useRef
current
const { remove: removeClickHandler } = useEventListener('click', ...);
No branches or pull requests
Thanks for the awesome hook!
Apologies if this is obvious but how would you remove the event listener? Let's say i'm attaching it based on the value of another variable..
The text was updated successfully, but these errors were encountered: