-
Is it ever unsafe to use a getter created with the If the answer is no, then I think this should be clearly added to the documentation. If the answer is yes (which is what I assume), then why not just build that safety into the decorator? For example:
I'm struggling to think of a use case where it would be desired to query the shadow DOM before at least the first update completes, but please educate me if there is one. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
I believe your use case is precisely why queryAsync exists. Your solution changes the return signature to be a Promise that must be awaited. But queryAsync does that already. In our solutions, we almost always use queryAsync, except when we have some sort of callback / event-based trigger where we know the element to be in the DOM. https://lit.dev/docs/components/shadow-dom/#@query-@queryall-and-@queryasync-decorators |
Beta Was this translation helpful? Give feedback.
-
This behavior is part of Lit's asynchronous rendering model. Lit's reactive update cycle is asynchronous because it generally results in ergonomic code that is naturally efficient. For example, where The tradeoff is that if you do have an API that cares about the rendered state of an element (as in your use case), you have to use Since the asynchronous model is core to the design and very important for efficiency, we have avoided adding APIs that automatically trigger synchronous rendering, preferring instead to recommend using Getting back to
Since this was a bit of a rough edge in some cases like this, we added
Hope that helps. |
Beta Was this translation helpful? Give feedback.
-
That becouse I never use |
Beta Was this translation helpful? Give feedback.
This behavior is part of Lit's asynchronous rendering model. Lit's reactive update cycle is asynchronous because it generally results in ergonomic code that is naturally efficient. For example, where
foo
andbar
are reactive propertiesel.foo = 5; el.bar = 6;
is simple to write and causes the element to efficiently update/render only once.The tradeoff is that if you do have an API that cares about the rendered state of an element (as in your use case), you have to use
await el.updateComplete
.Since the asynchronous model is core to the design and very important for efficiency, we have avoided adding APIs that automatically trigger synchronous rendering, preferring instead to recommend us…