Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/docs/with-resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>(() => `/user/${state.userId}`)),
withResource(({ userId }) => ({
list: httpResource<User[]>(() => '/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.
Expand Down Expand Up @@ -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
Expand Down