> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Optimistic updates

> Show a mutation's effect on watched values before it commits

`optimistic` edits this client’s live watches at head. A refusal removes
the edit immediately. After a successful mutation, the client keeps the
edit until every affected watch reaches the commit’s `version` or a later
version.

```tsx theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
const rename = useMutation(app.notes.rename);

await rename({ note, text }, {
  optimistic: (answers, { note, text }) =>
    answers.edit(app.notes.list, {}, (facts) =>
      facts.map((fact) => (fact.id === note ? { ...fact, text } : fact)),
    ),
});
```

`answers.edit(fn, args, update)` replaces the value of a query this client
is watching with those arguments. A query not watched with those arguments is
left alone. `update` receives the current value and returns the next one:
treat values as immutable and return a new one. The client may run the
callback again as new values arrive, so keep it pure and synchronous.

The same option works on `client.mutate`:

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
await client.mutate(app.notes.rename, { note, text }, {
  optimistic: (answers, args) =>
    answers.edit(app.notes.list, {}, (facts) =>
      facts.map((fact) => (fact.id === args.note ? { ...fact, text: args.text } : fact)),
    ),
});
```

Show the pending state with `rename.pending`, and read the mutation's
`Result` for a refusal: the edit is gone by then, so the list already shows
the committed value again.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.