Skip to content

Commit 25ee515

Browse files
docs(withResource): mention how to patch resource state (#282)
Co-authored-by: Rainer Hahnekamp <rainer.hahnekamp@gmail.com>
1 parent 89332b6 commit 25ee515

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

docs/docs/with-resource.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,26 @@ With named resources, each resource gets prefixed properties:
8383
- **Single resource:** use when your store works with just one data source.
8484
- **Named resources:** use when your store is larger and manages multiple entities or async operations.
8585

86+
## Updating
87+
88+
The state to patch corresponds directly to the name of the resource's value signal.
89+
90+
```ts
91+
const UserStore = signalStore(
92+
withResource((state) => httpResource<User>(() => `/user/${state.userId}`)),
93+
withResource(({ userId }) => ({
94+
list: httpResource<User[]>(() => '/users', { defaultValue: [] }),
95+
})),
96+
);
97+
98+
// Unnamed resource: `value`
99+
patchState(store, { value: { id: 1, name: 'John' } });
100+
101+
// Named resource: name prefix + `Value`
102+
// See the "Error Handling" section about why `listValue` has to be treated like it could be undefined.
103+
patchState(store, ({ listValue }) => ({ listValue: [...(listValue ?? []), { id: 1, name: 'John' }] }));
104+
```
105+
86106
## Error Handling
87107

88108
The error throwing behavior of the native `resource` causes a deadlock in the error case.
@@ -111,6 +131,10 @@ Options:
111131

112132
Under the hood, `'previous value'` and `'undefined value'` proxy the value. For a detailed explanation for why this is done, check out the [JSDoc for the error handling strategy](https://github.com/angular-architects/ngrx-toolkit/blob/main/libs/ngrx-toolkit/src/lib/with-resource.ts#L402).
113133

134+
The implications of `undefined value` is that the inferred value can be `undefined`, even if there is a `defaultValue` set for the resource.
135+
For example, in the [`Updating`](#updating) section, `listValue` will be inferred as `User[] | undefined`. To be able to infer the type with a guaranteed value,
136+
use `{ errorHandling: 'previous value' }` of `withResource` in conjunction with `defaultValue` of said resource.
137+
114138
## Component Usage
115139

116140
```typescript

0 commit comments

Comments
 (0)