-
Notifications
You must be signed in to change notification settings - Fork 880
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
[reactive-element] Avoid @query
caching null
results before the first render
#4237
Comments
@query
caching null
results before the first render@query
caching null
results before the first render
I think this sounds like a good idea. I wonder in what cases you're accessing the query field before first render though? |
@rictic thoughts? |
I feel like I've seen code kinda like: @query('button', true) button: HTMLButtonElement|undefined;
clickButton() {
this.button?.click();
} Is that the best? No. Should it cache |
It'd add few bytes, little overhead, and is otherwise a solid improvement, so I'm in favor. Even better with a dev mode warning for accessing a query before first updated. |
@rictic That's exactly the main use case that came to my mind. The other case is when queried properties are accessed in |
Should this be an RFC?
Which package is this a feature request for?
Lit Core (lit / lit-html / lit-element / reactive-element)
Description
If you access
@query
d property before the first render, if will yieldnull
but will staynull
is you set the cache flag totrue
. The documentation should probably warn the users that this might happen (might open a separate issue on lit.dev repo).I think it could be beneficial if
@query
caches the result only if it actually has a chance to return a non-null result, i.e. only ifhasUpdated
istrue
. Something like this:I'm available to open a PR for this.
Alternatives and Workarounds
No alternative that would be as simple as using
@query
straight away.One could redefine their own
@query
selector, but of course it'd need to be kept in line with Lit's decorator.Alternatively, one could check if
hasUpdated
istrue
if they need to access the queried property. A secondary getter could be handy, but adds overhead:The text was updated successfully, but these errors were encountered: