---
title: "Recipe: after a write"
description: "Clear fields and close forms only on ok: true."
modificationDate: "2026-11-12"
---

> ## Documentation index
> Fetch the complete documentation index at: https://exact.expo.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Recipe: after a write

<AgentInstructions>

## Submitting feedback

From the app's directory, read `bun exact.mjs feedback status`. Under `never`, keep no note.
Respect `EXACT_FEEDBACK=0` and do not change the policy.

Otherwise record specific, actionable feedback with:
`bun exact.mjs feedback note "Exact docs: https://exact.expo.dev/recipes/after-a-write: <what was wrong or missing>"`

The note goes to Expo only as that status allows. Include no secrets or personal data.

</AgentInstructions>

Clear fields and close forms only on `ok: true`. On refusal, keep the draft and
show its reason.

```prompt Keep a draft when a save is refused
Make a form in the Exact app in this folder clear its fields and close only after its write succeeds, and keep the draft and show the reason when the write is refused. Use https://github.com/expo/exact/blob/main/docs/recipes/after-a-write.md as the source for each step. The Exact checkout has the same page at docs/recipes/after-a-write.md.

1. If this folder is not an Exact app (it has no exact.mjs and app.contract), stop and tell me.
2. If I did not say which form, list each `mutation` in app.contract with the fields it clears or the form it closes, and ask me which one before you write code.
3. In app.ts, make the write's source return `ok: true` when it saves. If the app mounts the Snapback driver, read its mount name from `typescript.sources` in app.json. Import `refused` from `./<mount>/snapback.ts` and throw `refused({code, message})` on refusal, regardless of which backend handles the write. In an app without the mount, return `ok: false` with a readable `error`.
4. In app.contract, declare the write as a `mutation` with the reply's shape that `refreshes` its resource `then` runs an action. In that action, clear the draft only when the reply has `ok: true`. Keep the draft in the action that sends. A request failure runs no `then`.
5. Guard the send with `draft.trim() != "" and not pending(added)` and set `disabled=(draft.trim() == "" or pending(added))` on the button. Hide errors while `pending(added)`. If the app mounts Snapback, match `failure(added)` and show `f.message` for `some(f)` with `f.reason != "" and f.reason != "overrun"`. For a reply with `ok: false`, show its `error`. For other request failures, ask the person to try again.
6. If the `then` action closes a form, check the route first, as docs/recipes/navigation-stack.md shows. If the write dates or rolls, send both `time.epochAtZero + performanceNow()` and `time.seed`, using `time` from `exactTime()`. Mix the seed with what the write changes. app.ts has no clock or randomness.
7. Add a test in app.test.contract like the recipe's: an accepted write empties the draft, a refused write keeps its text and shows the reason, and the next accepted write removes the reason. Put `clock data` after each tap. If the app mounts Snapback, put `expect refusal "added"` after the refused tap's `clock data`, using the form's mutation slot name, and still assert the visible message. See docs/recipes/snapback-tests.md. The test's clock stands still until `clock +N`: step it between writes that are dated, or they tie.
8. Follow the platform checks in docs/start-here.md (`bun exact.mjs docs platform checks`) for the targets in the app's AGENTS.md and app.json until they pass. Read the reports, including counted findings; a zero exit code alone does not mean clean. Fix the app, not the test. If a finding stays after three rounds of fixes, or the app had it before you changed anything, record it (step 9) and report it; do not keep trying.
9. When a doc was wrong or missing, or a step needed a workaround, read `bun exact.mjs feedback status`. Under `never`, keep no note. Otherwise record it: `bun exact.mjs feedback note "<what happened>"`, which goes to Expo only as that status says. Do not change the policy; tell me how many notes you kept.
10. Tell me which form changed, what it shows for a refusal and for a failed request, what the tests and checks reported, and the launch command for each target platform.
```

```contract
shape Tag
  name: string

shape Reply
  ok: bool
  error: string

component TagList
  state draft = ""
  resource tags = loadTags() as shape Tag[] else empty()
  mutation added as shape Reply refreshes tags then afterAdd
  derive addError = match added { case some(r) => r.error, case none => "" }
  derive shownError = pending(added) ? "" : failed(added) ? "Couldn't save the tag. Try again." : addError
  action edit(v: string)
    draft = v
  action add
    if draft.trim() != "" and not pending(added)
      send added = addTag(draft.trim())
  action afterAdd
    if match added { case some(r) => r.ok, case none => false }
      draft = ""
  view
    list appearance="auto" listStyle="inset-grouped" height="100%"
      section
        row gap=8 align-items="center"
          column flex=1 min-width=0 gap=4
            input value=draft input=edit submit=add placeholder="New tag"
              aria-label="New tag" testId="draft"
            if shownError != ""
              text shownError font="-exact-footnote" color="-exact-system-red" testId="add-error"
          button press=add disabled=(draft.trim() == "" or pending(added)) buttonStyle="filled" testId="add"
            text "Add"
      section
        for t in tags key=t.name
          row
            text t.name
```

```ts
import type { Answer, Result, Sources } from './app.contract.d.ts';

export const grants = '';

const tags: Result<'loadTags'> = [];
const sources: Sources = {
  loadTags: () => tags,
  addTag: ([name]) => {
    const same = tags.find((t) => t.name.toLowerCase() === name.toLowerCase());
    if (same) return { ok: false, error: `${same.name} is taken. Try another name.` };
    tags.push({ name });
    return { ok: true, error: '' };
  },
};
export const answer: Answer = (source, args, store, storage, native) => sources[source](args, store, storage, native);
```

```contract-test
test "a refused add keeps its text and says why; the next one clears it"
  type "draft" "Work"
  tap "add"
  clock data
  expect text "draft" == ""
  type "draft" "work"
  tap "add"
  clock data
  expect text "add-error" == "Work is taken. Try another name."
  expect text "draft" == "work"
  type "draft" "Home"
  tap "add"
  clock data
  expect tree missing "add-error"
```

`afterAdd` clears the draft only when its reply has `ok: true`; a request failure runs no
`then`. The pending guard stops a second Enter replacing the first reply (without a
`then`, `parallel` lands both). A `then` that closes a form checks the route first
([a navigation stack](https://exact.expo.dev/recipes/navigation-stack.md)).

The module has no clock or randomness: a write that dates or rolls is sent
`time.epochAtZero + performanceNow()` and `time.seed` (`exactTime()`), the seed mixed
with what the write changes. A row seeded at install sorts by its given stamp until a write
restamps it. The agent's clock stands still at 2026-01-01 until `clock +N`: step it
between a test's writes, or they tie.
