Skip to content

Commit 6c02a5d

Browse files
authored
fix: add info about equality fn and fix some snippets (#34)
1 parent a13d493 commit 6c02a5d

File tree

1 file changed

+47
-24
lines changed

1 file changed

+47
-24
lines changed

src/pages/snippets/signal-reactivity-cheatsheet.mdx

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,68 +12,91 @@ Change detection works by detecting common browser events like mouse clicks, HTT
1212
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.
1313
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.
1414

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.
1616

1717
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.
1818

19-
#### Create a signal
19+
### Create a signal
2020

2121
Create a signal is very easy, it's just calling a function
2222

2323
```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;
2525
```
2626

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
2841

2942
The signal function return a SettableSignal. This gives you some amazing possibilities
3043

3144
```typescript
3245
const todos = signal([]);
3346

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
3548

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
3753

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
4158
```
4259

43-
#### Get the value of a signal
60+
### Get the value of a signal
4461

4562
The way to get the value of a signal will be the same in the template as in the component.
4663

4764
```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
5067
```
5168

52-
#### Computed
69+
### Computed
5370

5471
`computed()` creates a memoizing signal, which calculates its value from the values of some number of input signals.
5572

5673
```typescript
57-
const person = signal<{ firstname: string; lastname: string}>({ firstname: 'John', lastname: 'Doe'})
74+
const person = signal<{ firstname: string; lastname: string }>({
75+
firstname: "John",
76+
lastname: "Doe",
77+
});
5878

5979
// Automatically updates when person() change;
60-
const presentation = computed(() => console.log(`${person().firstname} ${person().lastname}`));
80+
const presentation = computed(() => {
81+
const fullName = `${person().firstname} ${person().lastname}`;
82+
console.log(fullName);
83+
return fullName;
84+
});
6185

62-
person.update(person => ({ firstname: 'Angular', lastname: 'Snippet'}));
86+
person.update((person) => ({ firstname: "Angular", lastname: "Snippet" }));
6387
```
6488

65-
#### Effect
89+
### Effect
6690

6791
`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.
6892

6993
```typescript
7094
const idTodo = signal(12);
7195

72-
effect(() =>
73-
fetch(`https://awesome-todo.com/todos/${idTodo()}`)
74-
.then(response => response.json())
75-
.then(console.log) // will sent the request with idTodo() = 12
76-
)
96+
effect(
97+
() =>
98+
fetch(`https://awesome-todo.com/todos/${idTodo()}`)
99+
.then((response) => response.json())
100+
.then(console.log) // will sent the request with idTodo() = 12
101+
);
77102
```
78-
79-

0 commit comments

Comments
 (0)