-
Basically, I’m implementing pagination with a query. I have page, pageSize, and query. Here is my code: import { parseAsString, parseAsInteger, useQueryStates, parseAsStringLiteral } from 'nuqs';
const createParsers = (query: string, page: number, pageSize: number) => ({
query: parseAsString.withDefault(query).withOptions({ history: 'push' }),
page: parseAsInteger.withDefault(page).withOptions({ history: 'push' }),
pageSize: parseAsInteger.withDefault(pageSize).withOptions({ history: 'push' }),
});
const urlKeys = {
page: 'page',
pageSize: 'pageSize',
};
export type ParsersType = ReturnType<typeof createParsers>;
export type OrganizationListSearchParams = ReturnType<typeof useQueryStates<ParsersType>>[0];
export type SetOrganizationListSearchParams = ReturnType<typeof useQueryStates<ParsersType>>[1];
export function useOrganizationListSearchParams(
query: string = '',
page: number = 1,
pageSize: number = 10,
) {
return useQueryStates(createParsers(query, page, pageSize), {
urlKeys,
});
} Now, I have another hook for data fetching where I’m using this hook. export function useGetLatestOrganization(backendApi: BackendApi) {
const [searchParams, updateOrganizationSearchParams] = useOrganizationListSearchParams();
const { query, page, pageSize, sortBy } = useMemo(() => searchParams, [searchParams]);
const { data, isLoading, error, isValidating, mutate } = useSWR<PageOrganization>(
['Organization', query, page, pageSize],
() =>
backendApi.organization
.getAllOrganizationsApiV1OrganizationGet(query, page, pageSize)
.then((res) => res.data),
swrOptions
);
return useMemo(
() => ({
mutate,
organizations: data?.items || [],
organizationsPage: data?.page || 0,
organizationsPageSize: data?.pageSize || 0,
organizationsTotalRecords: data?.total || 0,
organizationsTotalPages: data?.pages || 0,
organizationsLoading: isLoading,
organizationsError: error,
organizationsValidating: isValidating,
organizationsEmpty: !isLoading && !data?.items.length,
organizationSearchParams: { query, page, pageSize, sortBy },
updateOrganizationSearchParams,
}),
[
mutate,
data,
error,
isLoading,
isValidating,
query,
page,
pageSize,
sortBy,
updateOrganizationSearchParams,
]
);
} Now, the idea is that when the user tries to use the search bar, I update the query with page = 1. updateOrganizationSearchParams((previousState) => ({
query: event.target.value,
page: 1,
})); Here is the idea to set the default or trigger the page to 1 when the query changes, but only on one render. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
First off, I would recommend memoizing the parsers, as they are recreated on every render here, and therefore breaking referential equality on your state updater function: export function useOrganizationListSearchParams(
query: string = '',
page: number = 1,
pageSize: number = 10,
) {
const parsers = useMemo(() => createParsers(query, page, pageSize), [query, page, pageSize])
return useQueryStates(parsers, {
urlKeys,
});
} Now regarding your main question, I'm not sure I understand what you are trying to do, could you clarify please? |
Beta Was this translation helpful? Give feedback.
Thank you, I was looking for this.
Using a constant value like
1
is difficult to manage with default value, sonull
is a perfect solution.