File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed
ngrx.io/content/guide/signals
www/src/app/pages/guide/signals Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -54,6 +54,20 @@ console.log(firstName()); // logs: 'Eric'
54
54
console .log (lastName ()); // logs: 'Clapton'
55
55
```
56
56
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
+
57
71
<div class =" alert is-helpful " >
58
72
59
73
For enhanced performance, deeply nested signals are generated lazily and initialized only upon first access.
Original file line number Diff line number Diff line change @@ -54,6 +54,35 @@ console.log(firstName()); // logs: 'Eric'
54
54
console .log (lastName ()); // logs: 'Clapton'
55
55
```
56
56
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
+
57
86
<ngrx-docs-alert type =" help " >
58
87
59
88
For enhanced performance, deeply nested signals are generated lazily and initialized only upon first access.
You can’t perform that action at this time.
0 commit comments