Replies: 1 comment
-
Never mind got it working: import { batch, createMemo, createResource, createSignal } from "solid-js";
export function createPaginatedResource<T>(
fn: (page: string) => Promise<{ page: T[]; nextPageToken: string }>,
) {
const [pageToken, setPageToken] = createSignal<string>();
const [resource, { mutate }] = createResource(
() => fn(""),
async (p) => {
const v = await p;
setPageToken(v.nextPageToken);
return v.page;
},
);
const hasMore = createMemo(() => pageToken() !== "");
const loadMore = async () => {
const nextToken = pageToken();
if (!hasMore() || nextToken == undefined) {
return;
}
const nextPage = await fn(nextToken);
batch(() => {
setPageToken(nextPage.nextPageToken);
mutate((prev) => [...(prev ?? []), ...nextPage.page]);
});
};
return [resource, { hasMore, loadMore }] as const;
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey! With the
v0.10.0
APIs I am trying to understand how pagination can work.For page based pagination i.e. the page token can be part of the url and we can just navigate to the new url which should just refresh the data. This seems straightforward to me.
My use case is slightly different, I want my results to be part of an scrollable list like how social feeds are. Previously I just used
createResource
like this:I did not integrate this with the data functions of the previous router. With the newer APIs backed by a cache I was hoping to utilize the load option of the route along with
cache
to load the first page and continue loading something like this. But the docs saycache
won't work insidecreateResource
.Any idea how I can make use of
createAsync
andcache
here?Beta Was this translation helpful? Give feedback.
All reactions