-
Thanks for the nice work! Recently I'm experimenting the data router features but have some difficulties when implementing one of my requirements. I know that we could set query param in the URL to somehow pass parameters to the loader function. However, I would like it not being the part of URL. My use case is that, I'm doing pagination with the loader function, but the I have done some researches, but still cannot find a proper workaround.
I would love to hear any advices to handle this. Thank you :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
So far I've come up with this closest implementation, but I'm not sure if it's the correct way to achieve it. export async function loader() {
return await fetchList();
}
export async function action({ request }: ActionFunctionArgs) {
const formData = await request.formData();
if (formData.has("cursor")) {
const cursor = formData.get("cursor");
return await fetchList(cursor);
}
// When there's no `cursor` (the previous page button is going to the first page), don't fetch anything and simply use loader data
return null;
} export const routes = [
{
path: "/",
element: <ListPage />,
loader,
action,
shouldRevalidate: ({ formData, defaultShouldRevalidate }) => {
return defaultShouldRevalidate && !formData.has("cursor");
},
},
]; const loaderData = useLoaderData();
const actionData = useActionData();
const data = actionData || loaderData;
// Submit the `cursor` when clicking the next page button
<Form method="post">
<input type="hidden" name="cursor" value={data.cursor} />
<button>Next Page</button>
</Form>
// Read `data` which comes from either `action` or `loader` for building the list |
Beta Was this translation helpful? Give feedback.
What is the expectation if the user refreshes their browser? If you don't have the
cursor
or equivalent in the URL, it will reload data for the first page of the results. If that's OK, then your solution is fine.I'm unsure why you can't have the cursor in the URL. As for the previous cursors, those are stored in the browser's history stack, so you wouldn't need to save them explicitly.