diff --git a/docs/docs/with-resource.md b/docs/docs/with-resource.md index e023cea..0bc1158 100644 --- a/docs/docs/with-resource.md +++ b/docs/docs/with-resource.md @@ -83,6 +83,26 @@ With named resources, each resource gets prefixed properties: - **Single resource:** use when your store works with just one data source. - **Named resources:** use when your store is larger and manages multiple entities or async operations. +## Updating + +The state to patch corresponds directly to the name of the resource's value signal. + +```ts +const UserStore = signalStore( + withResource((state) => httpResource(() => `/user/${state.userId}`)), + withResource(({ userId }) => ({ + list: httpResource(() => '/users', { defaultValue: [] }), + })), +); + +// Unnamed resource: `value` +patchState(store, { value: { id: 1, name: 'John' } }); + +// Named resource: name prefix + `Value` +// See the "Error Handling" section about why `listValue` has to be treated like it could be undefined. +patchState(store, ({ listValue }) => ({ listValue: [...(listValue ?? []), { id: 1, name: 'John' }] })); +``` + ## Error Handling The error throwing behavior of the native `resource` causes a deadlock in the error case. @@ -111,6 +131,10 @@ Options: 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). +The implications of `undefined value` is that the inferred value can be `undefined`, even if there is a `defaultValue` set for the resource. +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, +use `{ errorHandling: 'previous value' }` of `withResource` in conjunction with `defaultValue` of said resource. + ## Component Usage ```typescript