-
Notifications
You must be signed in to change notification settings - Fork 319
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
[docs] Best practices for adding event listeners to a host element #139
Comments
Thanks for the question, I'm working on the docs for lit-element :) What's in my draft at the moment is basically this
This is based on the Polymer docs: https://www.polymer-project.org/3.0/docs/devguide/events. My understanding is that it's the same for lit-element, though I'll confirm this info before docs get published. |
Can you explain the third point a bit? Not sure I'm getting why that is the case, would be great to understand. As I understand it, constructor doesn't care about DOM whereas connectedCallback is after DOM attachment, so why would it matter for anything going to anything else than the actual component/children (DOM) to put it into connectedCallback? |
Can we get a preview somehow? There's very little info on on LitElement at the moment. Docs quality can make or break a project 100% |
@AndreasGalster the following is my attempt at an explanation. Maybe @TimvdLippe might be able to correct me or clarify here. I believe the reasoning for this recommendation is to do with removing the event listener in Scenario 1
Scenario 2
In Scenario 2, I am not sure when or how the memory in the scope created by the event listener on Window would get released. My understanding is that garbage collection works differently in different browsers and this scenario might not be good for memory management. Scenario 2.A
The recommendation to use Scenario 3
|
@aadamsx I agree, and will try to publish a preview as soon as I have enough content :) |
@katejeffreys That sounds about right! 😄 I think the key scenario is 2.A here, where the usage of |
Isn't there one more scenario where an event listener gets added - namely using It seems to me that this approach is easier than trying to add one via |
@akc42 The |
@TimvdLippe not anymore, it is |
I'd really love to see the readme.md updated with an actual useful example of event binding. Right now the only example is a Edit: Man, that was fast! Thanks for the update! |
I edited my previous comment, but since that won't trigger any notifications, I'll copy what I added here: Thanks for the quick update with the much more useful event handler example! |
Had to do quite a bit of digging to find this. Thanks for the clarification. Would love to see an example for this in the docs. |
@jeremyrader The docs are work in progress, and coming soon. If you're looking for some code examples maybe this is helpful to you. |
If you need to imperatively add a listener to rendered children, it must be added in |
As along as we don't add a listener to anything outside our scope we don't have to manually clean it up: - lit/lit-element#139 (comment) - https://polymer-library.polymer-project.org/3.0/docs/devguide/events - https://stackoverflow.com/a/42818539
There are three ways to add event listeners to a host element
ready
connectedCallback
(requires removal indisconnectedCallback
)Which is the best way? Are there any memory leak issues for (1) and (2)?
I hope there would be some documentation on this topic.
The text was updated successfully, but these errors were encountered: