Skip to content

Commit 26c18a2

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

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

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)