Skip to content
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

Support for event listener options #31

Closed
heokhe opened this issue Jun 12, 2020 · 4 comments
Closed

Support for event listener options #31

heokhe opened this issue Jun 12, 2020 · 4 comments
Assignees

Comments

@heokhe
Copy link

heokhe commented Jun 12, 2020

Can we have something like this?

useEventListener('click', () => {
  console.log('The window is clicked and cannot be clicked anymore');
}, { once: true });
@srmagura srmagura self-assigned this Jun 12, 2020
@srmagura
Copy link
Collaborator

Hey @hkh12, my fork @srmagura/use-event-listener already supports the other two addEventListener options: capture and passive.

I did not add the once option because I don't think it makes sense in the context of hooks. Hooks run after every render so it's a bit weird to have a hook that only does something a single time.

Instead, you can do:

useEffect(() => {
    window.addEventListener('click', fn, { once: true })
}, [])

There are other ways to only add the event listener once, depending on your use case.

@heokhe
Copy link
Author

heokhe commented Jun 12, 2020

I just used once as an example, but thanks anyway!

@heokhe heokhe closed this as completed Jun 12, 2020
@srmagura
Copy link
Collaborator

Going to leave this open and pin it until addEventListener options are added to this repo ;)

@srmagura srmagura reopened this Jun 14, 2020
@srmagura srmagura pinned this issue Jun 14, 2020
@donavon
Copy link
Owner

donavon commented Jul 23, 2020

deployed to npm as @use-it/event-listener@0.1.4

@donavon donavon closed this as completed Jul 23, 2020
@donavon donavon unpinned this issue Nov 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants