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: src/pages/snippets/signal-reactivity-cheatsheet.mdx
+47-24Lines changed: 47 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -12,68 +12,91 @@ Change detection works by detecting common browser events like mouse clicks, HTT
12
12
By default, Angular uses the _ChangeDetectionStrategy.Default_ change detection strategy which checks every component in the component tree from the top to the bottom every time a common browser event is emitted.
13
13
This way of checking don't take into account the components' dependencies and is called dirty checking. This can negatively influence your application performance specially for large application with many components.
14
14
15
-
With the release of Angular version 16, a new system of primitive reactivity will be created: Signal Reactivity which don't need zone js to trigger a refresh of a view.
15
+
With the release of Angular version 16, a new system of primitive reactivity will be created: Signal Reactivity which don't need zone js to trigger a refresh of a view.
16
16
17
17
The goal of this snippet is to give you a cheat sheet about signal reactivity and give you keys to be ready to work with them.
18
18
19
-
####Create a signal
19
+
### Create a signal
20
20
21
21
Create a signal is very easy, it's just calling a function
22
22
23
23
```typescript
24
-
const counter =signal(0) // create a signal with initial value to 0;
24
+
const counter =signal(0);// create a signal with initial value to 0;
25
25
```
26
26
27
-
#### Update a signal
27
+
#### Equality Function
28
+
29
+
By default, `signal()` uses a default `ValueEqualityFn`.
30
+
31
+
- For primitive values, `Object.is()` is utilized
32
+
- For reference values (object and array), they are always considered _unequal_
33
+
34
+
We can also provide a custom `ValueEqualityFn` to `signal()` for custom compare logic on Signal's update
35
+
36
+
```ts
37
+
const users =signal<User[]>([], (a, b) =>userCompareFn(a, b));
38
+
```
39
+
40
+
### Update a signal
28
41
29
42
The signal function return a SettableSignal. This gives you some amazing possibilities
30
43
31
44
```typescript
32
45
const todos =signal([]);
33
46
34
-
todos.set(['Make My dinner'])// directly set the signal to a new value and notify all the dependents
47
+
todos.set(["Make My dinner"]);// directly set the signal to a new value and notify all the dependents
35
48
36
-
todos.update(currentTodos=> ([...currentTodos, 'Create a new Angular Snippet']) ) // update the value of the signal based on its current value and notify all dependents
49
+
todos.update((currentTodos) => [
50
+
...currentTodos,
51
+
"Create a new Angular Snippet",
52
+
]); // update the value of the signal based on its current value and notify all dependents
37
53
38
-
todos.mutate(currentTodos=> {
39
-
currentTodos.push('Participate to Open Source');
40
-
}) // update the currentValue by mutating it in-place and notify all the dependents
54
+
// NOTE: `mutate` will by-pass equality check and will always notify change
55
+
todos.mutate((currentTodos) => {
56
+
currentTodos.push("Participate to Open Source");
57
+
}); // update the currentValue by mutating it in-place and notify all the dependents
41
58
```
42
59
43
-
####Get the value of a signal
60
+
### Get the value of a signal
44
61
45
62
The way to get the value of a signal will be the same in the template as in the component.
46
63
47
64
```typescript
48
-
const counter =signal(0) // create a signal with initial value
49
-
console.log(counter()) // print the value of the signal which is 0
65
+
const counter =signal(0);// create a signal with initial value
66
+
console.log(counter());// print the value of the signal which is 0
50
67
```
51
68
52
-
####Computed
69
+
### Computed
53
70
54
71
`computed()` creates a memoizing signal, which calculates its value from the values of some number of input signals.
55
72
56
73
```typescript
57
-
const person =signal<{ firstname:string; lastname:string}>({ firstname: 'John', lastname: 'Doe'})
74
+
const person =signal<{ firstname:string; lastname:string }>({
`effect()` schedules and runs a side-effectful function inside a reactive context. Signal dependencies of this function are captured, and the side effect is re-executed whenever any of its dependencies produces a new value.
0 commit comments