Skip to content

Commit c07f507

Browse files
authored
Merge pull request #62 from github/docs-add-lifecycle-methods
docs: add guide page about lifecycle hooks
2 parents 1026141 + b0482ea commit c07f507

4 files changed

Lines changed: 33 additions & 3 deletions

File tree

docs/_guide/anti-patterns.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
chapter: 9
2+
chapter: 10
33
subtitle: Anti Patterns
44
---
55

docs/_guide/conventions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
chapter: 7
2+
chapter: 8
33
subtitle: Conventions
44
---
55

docs/_guide/lifecycle-hooks.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
chapter: 7
3+
subtitle: Observing the life cycle of an element
4+
---
5+
6+
Catalyst Controllers - like many other frameworks - have several "well known" method names which are called periodically through the life cycle of the element, and let you observe when an element changes in various ways. Here is a comprehensive list of all life-cycle callbacks. Each one is suffixed `Callback`, to denote that it will be called by the framework.
7+
8+
### `connectedCallback()`
9+
10+
The [`connectedCallback()` is part of Custom Elements][ce-callbacks], and gets fired as soon as your element is _appended_ to the DOM. This callback is a good time to initialize any variables, perhaps add some global event listeners, or start making any early network requests.
11+
12+
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.
13+
14+
### `attributeChangedCallback()`
15+
16+
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:
17+
18+
```typescript
19+
attributeChangedCallback(name: string, oldValue: string|null, newValue: string|null): void {}
20+
```
21+
22+
### `disconnectedCallback()`
23+
24+
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.
25+
26+
### `adoptedCallback()`
27+
28+
The [`adoptedCallback()` is part of Custom Elements][ce-callbacks], and gets called when your element moves from one `document` to another (such as an iframe). It's very unlikely to occur, you'll almost never need this.
29+
30+
[ce-callbacks]: https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements#Using_the_lifecycle_callbacks

docs/_guide/patterns.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
chapter: 8
2+
chapter: 9
33
subtitle: Patterns
44
---
55

0 commit comments

Comments
 (0)