Skip to content

Commit ee35967

Browse files
authored
Merge pull request #118 from github/docs-add-things-to-remember-in-lifecycle-callbacks
docs: add "things to remember" in lifecycle callbacks
2 parents 33850e9 + 8e639a8 commit ee35967

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

docs/_guide/lifecycle-hooks.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ The [`connectedCallback()` is part of Custom Elements][ce-callbacks], and gets f
1111

1212
JavaScript traditionally uses the `constructor()` callback to listen for class creation. While this still works for Custom Elements, it is best avoided as the element won't be in the DOM when `constructor()` is fired, limiting its utility.
1313

14+
#### Things to remember
15+
16+
The `connectedCallback` is called _as soon as_ the element is attached to a `document`. This _may_ occur _before_ an element has any children appended to it, so you should be careful not expect an element to have children during a `connectedCallback` call. This means avoiding checking any `target`s or using other methods like `querySelector`. Instead use this function to initialize itself and avoid doing initialization work which depend on children existing.
17+
18+
If your element depends heavily on its children existing, consider adding a [`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) in the `connectedCallback` to track when your elements children change.
19+
1420
### `attributeChangedCallback()`
1521

1622
The [`attributeChangedCallback()` is part of Custom Elements][ce-callbacks], and gets fired when _observed attributes_ are added, changed, or removed from your element. It required you set a `static observedAttributes` array on your class, the values of which will be any attributes that will be observed for mutations. This is given a set of arguments, the signature of your function should be:
@@ -19,6 +25,10 @@ The [`attributeChangedCallback()` is part of Custom Elements][ce-callbacks], and
1925
attributeChangedCallback(name: string, oldValue: string|null, newValue: string|null): void {}
2026
```
2127

28+
#### Things to remember
29+
30+
The `attributeChangedCallback` will fire whenever `setAttribute` is called with an observed attribute, even if the _new_ value is the same as the _old_ value. In other words, it is possible for `attributeChangedCallback` to be called when `oldValue === newValue`. In most cases this really won't matter much, and in some cases this is very helpful; but sometimes this can bite, especially if you have [non-idempotent](https://en.wikipedia.org/wiki/Idempotence#Computer_science_examples) code inside your `attributeChangedCallback`. Try to make sure operations inside `attributeChangedCallback` are idempotent, or perhaps consider adding a check to ensure `oldValue !== newValue` before performing operations which may be sensitive to this.
31+
2232
### `disconnectedCallback()`
2333

2434
The [`disconnectedCallback()` is part of Custom Elements][ce-callbacks], and gets fired as soon as your element is _removed_ from the DOM. Event listeners will automatically be cleaned up, and memory will be freed automatically from JavaScript, so you're unlikely to need this callback for much.

0 commit comments

Comments
 (0)