Sub topic 10
Updating data locally with set and update
This chapter assumes you know how resource() loads data from params and loader, and that you can read value() and status(). Here you will change a resource's value without sending a request.
Why write to a resource
A resource normally gets its value from the loader. Sometimes you already know the new value, for example after the user adds an item and the server accepted it. Calling reload() for that costs another request. A local write puts the known value into the resource right away.
A resource created with resource() is writable. It has two write methods:
set(value)replaces the current value.update(fn)computes the new value from the current one. This is likesignal.update().
A working example
The component below loads todos, then lets the user add and remove items locally.
import { Component, resource } from '@angular/core';
interface Todo {
id: number;
title: string;
}
@Component({
selector: 'app-local-todos',
template: `
<button (click)="add()">Add</button>
<button (click)="clear()">Clear</button>
<button (click)="todos.reload()">Reload</button>
<p>Status: {{ todos.status() }}, {{ todos.value().length }} todos</p>
<ul>
@for (todo of todos.value(); track todo.id) {
<li>{{ todo.title }} <button (click)="remove(todo.id)">x</button></li>
}
</ul>
`,
})
export class LocalTodosComponent {
readonly todos = resource({
defaultValue: [] as Todo[],
loader: async ({ abortSignal }) => {
const res = await fetch('/api/todos', { signal: abortSignal });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return (await res.json()) as Todo[];
},
});
add() {
this.todos.update((list) => [...list, { id: Date.now(), title: 'New todo' }]);
}
remove(id: number) {
this.todos.update((list) => list.filter((todo) => todo.id !== id));
}
clear() {
this.todos.set([]);
}
}Each update() callback returns a new array. It does not push into the old one. Signals detect a change by reference, so mutating the array in place would not notify the template.
What happens to the status
After set() or update(), the resource's status becomes local. The value is the one you wrote, isLoading() is false, and no request was sent. The Status: line in the template shows this.
| Action | Status afterwards | Request sent |
|---|---|---|
| Loader finishes | `resolved` | Yes |
| `set()` or `update()` | `local` | No |
| `reload()` | `reloading`, then `resolved` | Yes |
| A signal read in `params` changes | `loading`, then `resolved` | Yes |
Common errors
A local value is temporary. Any new load replaces it, whether it comes from reload() or from a change in params. If the user's edit must survive, send it to the server too, or keep it in a separate signal.
A local write also cancels a load that is still running. The resource aborts that request, so the slow response cannot overwrite your value when it arrives.
Without defaultValue, value() is T | undefined and the update() callback receives undefined before the first load. Either set a defaultValue, as above, or handle undefined in the callback.
Exercise
A user clicks "Add", which calls update() on a todos resource. Then they click "Reload". What are the resource's statuses in order, and what happens to the added todo?
Show answer
The status is local after the update, then reloading while the loader runs, then resolved. The added todo disappears unless the server returned it, because the loader's result replaces the local value.