Skip to content

Commit 103bcc3

Browse files
ryansolidclaude
andauthored
docs: when to use createDeferred instead of latest or createMemo (#1693)
Adds a "Let a panel lag" section to the async reactivity concept page, a row for it in the update decision table, and caveats on the reference page contrasting it with reading every consumer through latest (solidjs/solid#4773). Claude-Session: https://claude.ai/code/session_0117BoWNSSgtemsSYsaX2hXE Co-authored-by: Claude <noreply@anthropic.com>
1 parent 8d358af commit 103bcc3

3 files changed

Lines changed: 58 additions & 5 deletions

File tree

‎scripts/extract-solid-ref.mjs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ const CANONICAL_ROUTES = {
104104
createEffect: ["reactivity/create-effect.mdx", "Reactivity"],
105105
createDeferred: ["reactivity/create-deferred.mdx", "Reactivity"],
106106
createMemo: ["reactivity/create-memo.mdx", "Reactivity"],
107+
createDeferred: [
108+
"`createDeferred` is not `latest` on every consumer. `latest` shows a value now while the update stays held for every other reader; a deferred memo is not part of the update, so nothing waits for it. See [Let a panel lag](/concepts/async-reactivity#let-a-panel-lag-createdeferred).",
109+
"Use it for data that may fall behind the rest of the page without making it wrong, such as an independent widget or a feed. Data that must agree with the rest of the page belongs in `createMemo`.",
110+
"Its first load still suspends to the nearest `Loading` boundary, unless `loadingValue` is set.",
111+
],
107112
createOptimistic: ["reactivity/create-optimistic.mdx", "Reactivity"],
108113
createSignal: ["reactivity/create-signal.mdx", "Reactivity"],
109114
flush: ["reactivity/flush.mdx", "Reactivity"],

‎src/routes/(2)concepts/(3)async-reactivity.mdx‎

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,17 @@ The question to ask is about the update, not about any one request, and it is on
241241

242242
**While this update is in flight, what should the user see?**
243243

244-
| The user should see… | Use |
245-
| ---------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
246-
| Nothing change until everything that depends on the input is ready | The default. Add [`isPending`](#another-answer-is-coming-ispending) to acknowledge the click. |
247-
| The control they touched reflect their input now, and the content wait | [`latest`](#show-the-input-now-latest) on the control, `isPending` to dim the content. |
248-
| A placeholder in place of the content for the changed subject | [`<Loading on={key}>`](#show-a-placeholder-again-loading-on) around that content. |
244+
| The user should see… | Use |
245+
| ------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------- |
246+
| Nothing change until everything that depends on the input is ready | The default. Add [`isPending`](#another-answer-is-coming-ispending) to acknowledge the click. |
247+
| The control they touched reflect their input now, and the content wait | [`latest`](#show-the-input-now-latest) on the control, `isPending` to dim the content. |
248+
| A placeholder in place of the content for the changed subject | [`<Loading on={key}>`](#show-a-placeholder-again-loading-on) around that content. |
249+
| The input and the rest of the page change now, and one panel keep its old content until its own answer lands | [`createDeferred`](#let-a-panel-lag-createdeferred) for that panel's data, `isPending` to dim it. |
249250

250251
The first two rows are the same screen: the tab highlight moves at once, the content stays and dims, the content swaps when it is ready.
251252
That is the standard interaction for a held update, and the rest of this section shows the pieces.
252253
The third row is for content that should not stay on screen when its subject changes, such as a panel for a different account.
254+
The fourth row is for a panel that may fall behind the rest of the page without making it wrong, such as the ten-second audit log.
253255

254256
The hold applies only to updates.
255257
Each panel's first load still reaches the nearest `Loading` boundary, and nested boundaries let panels appear as their own first data arrives.
@@ -389,6 +391,46 @@ This is the everyday shape of a held update, not a special case.
389391
Reading data through `latest` shows a result before the rest of the update has agreed on it, so a panel can show the new period's numbers while the selector still shows the old period.
390392
When you want the control to lead, use `latest` on the control; when you want a panel to show a placeholder, use the next section.
391393

394+
## Let a panel lag: `createDeferred`
395+
396+
`latest` changes what one reader shows.
397+
The update is still held: every plain reader of the input waits, and the page commits when the slowest answer lands.
398+
When a panel may keep its previous answer while the next one loads, and the rest of the page should not wait for it, create the panel's data with [`createDeferred`](/reference/solid-js/reactivity/create-deferred) instead of `createMemo`:
399+
400+
```tsx
401+
function AuditPanel(props: { period: string }) {
402+
const entries = createDeferred(() => fetchAudit(props.period));
403+
404+
return (
405+
<ul class={{ stale: isPending(() => entries()) }}>
406+
<For each={entries()}>{(entry) => <li>{entry.text}</li>}</For>
407+
</ul>
408+
);
409+
}
410+
```
411+
412+
After its first answer, a refetch of a deferred memo holds nothing.
413+
When the period changes, the selector and the other two panels change at once.
414+
The audit panel keeps its old entries, dims through `isPending`, and shows the new entries when its own request lands.
415+
416+
The two APIs look alike but answer different questions:
417+
418+
- `latest` is on the reader. This control shows the input now; the content still waits with the update.
419+
- `createDeferred` is on the source. No reader waits for this answer, so the update does not wait for it either.
420+
421+
So `createDeferred` is not the same as reading every consumer through `latest`.
422+
With `latest`, the update is still held for every plain read of the input until the slow answer lands.
423+
With `createDeferred`, the slow answer is no longer part of the update at all.
424+
425+
Reach for `createDeferred` when the async work can lag without making the page wrong: an independent widget, a feed, or search suggestions.
426+
Keep `createMemo` for data that must agree with the rest of the page, such as totals that must add up or the details of the selected item.
427+
428+
:::note
429+
A deferred memo may lag, but it never leads.
430+
Its first load is a first load like any other: readers suspend to the nearest `Loading` boundary.
431+
A refetch that reads a value another update is still holding lands with that update, so the panel never shows an answer the rest of the page has not committed to.
432+
:::
433+
392434
## Show a placeholder again: `Loading on`
393435

394436
Once a `Loading` boundary has shown content, a later update does not bring the fallback back; the content stays and the update is held.

‎src/routes/reference/(1)solid-js/(1)reactivity/create-deferred.mdx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ const rows = createDeferred(() => fetchRows(store.period));
6464
</section>;
6565
```
6666

67+
## Caveats
68+
69+
- `createDeferred` is not `latest` on every consumer. `latest` shows a value now while the update stays held for every other reader; a deferred memo is not part of the update, so nothing waits for it. See [Let a panel lag](/concepts/async-reactivity#let-a-panel-lag-createdeferred).
70+
- Use it for data that may fall behind the rest of the page without making it wrong, such as an independent widget or a feed. Data that must agree with the rest of the page belongs in `createMemo`.
71+
- Its first load still suspends to the nearest `Loading` boundary, unless `loadingValue` is set.
72+
6773
## Learn more
6874

6975
- [Reactivity](/concepts/reactivity)

0 commit comments

Comments
 (0)