Replies: 1 comment
|
Expected, and in your exact snippet it is worse than "fetches all pages": it never stops.
So the answer to question 1 is yes, tracked reads work the same in an async memo, and to question 2 also yes, do not write a signal the memo itself reads. Solid says so directly on the server build, where the same code prints: For cursor pagination, let the cursor be an input driven by an action, and let the memo return the cursor instead of assigning it: const [cursor, setCursor] = createSignal(undefined);
const [all, setAll] = createSignal([]);
const page = createMemo(async () => await fetchAlbums(cursor()));
createEffect(() => page(), (p) => setAll(prev => [...prev, ...p.data]));
const nextPage = () => {
const p = untrack(() => { try { return page(); } catch { return null; } });
if (p?.nextCursor) setCursor(p.nextCursor);
};Same fake API, three clicks: The Both runs are on solid-js 2.0.0-rc.3 under node with |
Uh oh!
There was an error while loading. Please reload this page.
Async
createMemowith pagination keeps re-running whennextCursoris updated from the fetched resultDescription
I'm testing the async
createMemobehavior in Solid 2.0.I have a paginated API where each response contains a
nextCursor. The cursor is stored in a signal and is also used as a dependency of an asynccreateMemo.After each request, I update the cursor with the
nextCursorreturned by the API.Something similar to:
Because
cursor()is read inside the memo, changing it after the request causes the memo to run again.As a result, the behavior becomes:
So simply reading the first result ends up fetching all pages until the API returns
nextCursor: null.Question
Is this the expected behavior for async
createMemoin Solid 2.0?I understand that
cursor()becomes a reactive dependency of the memo, so from the fine-grained reactivity perspective the reruns make sense.However, since Solid 2.0 encourages using async
createMemodirectly for data fetching, I'm wondering whether updating a pagination signal as a consequence of the same request is expected to recursively invalidate the memo in this way.Should pagination state instead always be updated outside of the memo, for example in response to an explicit "Next page" action?
Or is there a recommended Solid 2.0 pattern for this type of cursor-based pagination?
Expected behavior / clarification requested
I'm mainly looking for clarification on whether:
createMemo.Environment
All reactions