You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/lit-dev-content/site/docs/v3/components/properties.md
+22-2Lines changed: 22 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -129,10 +129,13 @@ class MyElement extends LitElement {
129
129
}
130
130
```
131
131
132
-
In **JavaScript**, you **must not use class fields** when declaring reactive properties. Instead, properties must be initialized in the element constructor:
132
+
In **JavaScript**, you **must not use class fields** when declaring reactive properties. Instead, properties must be either initialized in the element constructor or set with the `defaultValue` option:
133
133
```js
134
134
classMyElementextendsLitElement {
135
-
static properties = {foo: {type:String}}
135
+
static properties = {
136
+
foo: {type:String},
137
+
bar: {type:String, defaultValue:'Default'}
138
+
}
136
139
constructor() {
137
140
super();
138
141
this.foo='Default';
@@ -219,6 +222,18 @@ A [custom converter](#conversion-converter) for converting between properties an
219
222
</dd>
220
223
<dt>
221
224
225
+
`defaultValue`
226
+
227
+
</dt>
228
+
<dd>
229
+
230
+
When set, the property is initialized to this value and if the `reflect` option is `true`, the initial default value does *not* reflect. Subsequent changes to the property will reflect, even if they are equal to the default value.
231
+
232
+
Avoid setting both a default value and defining a field initializer or setting the property in the constructor or connectedCallback.
233
+
234
+
</dd>
235
+
<dt>
236
+
222
237
`hasChanged`
223
238
224
239
</dt>
@@ -553,11 +568,16 @@ If this behavior doesn't fit your use case, there are a couple of options:
553
568
554
569
You can configure a property so that whenever it changes, its value is reflected to its [corresponding attribute](#observed-attributes). Reflected attributes are useful because attributes are visible to CSS, and to DOM APIs like `querySelector`.
555
570
571
+
Note, if you set a `defaultValue` for the property, this value will *not* be initially reflected to the corresponding attribute. All subsequent changes will be reflected. This matches web platform behavior for attributes like `id`. For example, the default value of an element's `id` property is `''` (an empty string) and initially it does not have an `id` attribute, but if the `id` property is set (even to an empty string), the appropirate `id` attribute is reflected.
572
+
556
573
For example:
557
574
558
575
```js
559
576
// Value of property "active" will reflect to attribute "active"
560
577
active: {reflect:true}
578
+
// Value of property "variant" will reflect except that it will not
579
+
// be initialized to the default value of `normal`.
580
+
variant: {reflect:true, defaultValue:'normal'}
561
581
```
562
582
563
583
When the property changes, Lit sets the corresponding attribute value as described in [Using the default converter](#conversion-type) or [Providing a custom converter](#conversion-converter).
0 commit comments