-
Notifications
You must be signed in to change notification settings - Fork 186
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
[doc] Add accessor
solution option for solving the reactive updates class field error
#1243
Comments
accessor
solution option for solving the reactive updates class field error
I think we should also remove the section about compiling with Babel. https://lit.dev/docs/components/properties/#avoiding-issues-with-class-fields:~:text=When%20compiling%20JavaScript%20with%20Babel While we're at it, I also would like to see the earlier section on JavaScript clarified a bit (https://lit.dev/docs/components/properties/#avoiding-issues-with-class-fields:~:text=In%20JavaScript%20you%20must%20not%20use%20class%20fields). We should show what the wrong syntax is. Just saying "must not use class fields", and showing the constructor code is unclear to me. Proposal: class MyElement extends LitElement {
static properties = {foo: {type: String}}
foo = 'Default'; // ❌ this will make `foo` not reactive
} class MyElement extends LitElement {
static properties = {foo: {type: String}}
constructor() {
super();
this.foo = 'Default'; // ✅ this is good
}
} |
additionally, the following is also valid: class MyElement extends LitElement {
static properties = {foo: {type: String}}
accessor foo = 'Default'; // ✅ this is good
} Thoughts about including? |
Oh, my god, it does work. But only with Babel though so I don't think we should include this for pure JS solution. And.. doing |
It also works in TypeScript if you're avoiding using decorators and have |
Relevant potential change suggested here too: |
When using TypeScript, if
useDefineForClassFields
istrue
(Note default istrue
if target is ES2022 or higher, including ESNext;false
otherwise)Then setting class fields will throw an error.
E.g.
Throws:
Issue
The error link: https://lit.dev/msg/class-field-shadowing doesn't document that a possible solution is to use the
accessor
keyword (which is forward compatible with standard decorators).This is a valid solution in Lit 3 with hybrid decorators.
Proposal
It should be documented that the error can be solved with
This code works with all combinations of
experimentalDecorators
anduseDefineForClassFields
and is forward compatible with standard decorators.The text was updated successfully, but these errors were encountered: