-
Notifications
You must be signed in to change notification settings - Fork 88
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
connect: addEventListener on components #20
Comments
There are two options with event listeners. For example, render factory uses globally attached event listener to trigger re-render all of the elements. For you it could look like this: const set = new Set();
// In this case your event listener is attach once, but it is always there
// It is proper pattern if you factory do something in sync between instances
document.addEventListener('event-name', () => {
set.forEach(...);
});
export function myFactory() {
return {
get: ...
connect: (host) => {
set.add(host);
return () => set.remove(host);
},
};
} The second option is to attach event listener in export function myFactory() {
return {
get: ...
connect: (host) => {
// holding reference to the callback is most important here,
// as it has to be removed in disconnect
const cb = () => { ... };
document.addEventListener('event-name', cb);
return () => document.removeEventListener('event-name', cb);
},
};
} Also, you might want to look at the built-in factories source code. You can find there examples of how to add event listeners. About docs - I am working on it right now (you can sneak peek on |
It worked! TY for the help. |
You can use import { dispatch } from 'hybrids';
dispatch(document, 'event-name', { detail: { value: 0 } }); It creates CustomEvent for you, passes options and dispatches it. |
It gave me an error |
The name isn't dispatchEvent, but dispatch |
Yes, you are right. I was writing it on my phone, fixed comment with the proper name. |
I wanted to know if there is a way of adding a eventListener to the component that listen do "document". I've tried doing something that looked like redux connect function but I wasn't able to make it work.
Could you provide some example? Or at least put it on the docs,
The text was updated successfully, but these errors were encountered: