Skip to content

Commit 5f159a1

Browse files
docs(signals): add user-defined signals for signalState
1 parent 8fd5c51 commit 5f159a1

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

projects/ngrx.io/content/guide/signals/signal-state.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ console.log(firstName()); // logs: 'Eric'
5454
console.log(lastName()); // logs: 'Clapton'
5555
```
5656

57+
When a state property holds an object as its value, the `signalState` function generates a `DeepSignal`.
58+
It can be used as a regular read-only signal, but it also contains signals for each property of the object it refers to.
59+
60+
```ts
61+
const firstName = user.firstName; // type: Signal<string>
62+
const lastName = user.lastName; // type: Signal<string>
63+
64+
console.log(firstName()); // logs: 'Eric'
65+
console.log(lastName()); // logs: 'Clapton'
66+
```
67+
68+
If the root properties of a state are already of type `WritableSignal`, they will be reused, instead of creating new signals.
69+
This allows an integration of external `WritableSignal`s — such as `linkedSignal` or `resource.value`.
70+
5771
<div class="alert is-helpful">
5872

5973
For enhanced performance, deeply nested signals are generated lazily and initialized only upon first access.

projects/www/src/app/pages/guide/signals/signal-state.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,35 @@ console.log(firstName()); // logs: 'Eric'
5454
console.log(lastName()); // logs: 'Clapton'
5555
```
5656

57+
If the root properties of a state are already of type `WritableSignal`, they will be reused, instead of creating new signals.
58+
This allows an integration of external `WritableSignal`s — such as `linkedSignal` or `resource.value`.
59+
60+
```ts
61+
import { linkedSignal } from '@angular/core';
62+
import { signalState } from '@ngrx/signals';
63+
import { User } from './user.model';
64+
65+
const referenceId = signal(1);
66+
67+
const userState = signalState<UserState>({
68+
user: linkedSignal<User>(() => ({
69+
id: referenceId(),
70+
firstName: '',
71+
lastName: '',
72+
})),
73+
isAdmin: false,
74+
});
75+
76+
console.log(userState.user()); // logs: { id: 1, firstName: '', lastName: '' }
77+
patchState(userState, {
78+
user: { id: 2, firstName: 'Brian', lastName: 'May' },
79+
});
80+
console.log(userState.user()); // logs: { id: 2, firstName: 'Brian', lastName: 'May' }
81+
82+
referenceId.set(3);
83+
console.log(userState.user()); // logs: { id: 3, firstName: '', lastName: '' }
84+
```
85+
5786
<ngrx-docs-alert type="help">
5887

5988
For enhanced performance, deeply nested signals are generated lazily and initialized only upon first access.

0 commit comments

Comments
 (0)