react-query#InfiniteData TypeScript Examples
The following examples show how to use
react-query#InfiniteData.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useBackgroundPaginatedRequest.ts From apps with GNU Affero General Public License v3.0 | 6 votes |
useBackgroundPaginatedRequest = <T extends InfiniteData<unknown>>(
queryKey: QueryKey,
): T => {
const client = useQueryClient();
const data = client.getQueryData<T>(queryKey);
useBackgroundRequest(queryKey, {
callback: ({ res, req }) => {
if (!res) {
return;
}
const current = client.getQueryData(queryKey) as T;
const updated = { ...current } as T;
const index = updated.pages.length - 1;
updated.pageParams[index] = req.variables.after;
updated.pages[index] = res;
client.setQueryData(queryKey, updated);
},
});
return data;
}
Example #2
Source File: useFeed.ts From apps with GNU Affero General Public License v3.0 | 6 votes |
updateCachedPage = (
feedQueryKey: unknown[],
queryClient: QueryClient,
pageIndex: number,
manipulate: (page: Connection<Post>) => Connection<Post>,
): void => {
queryClient.setQueryData<InfiniteData<FeedData>>(
feedQueryKey,
(currentData) => {
const { pages } = currentData;
const currentPage = cloneDeep(pages[pageIndex]);
currentPage.page = manipulate(currentPage.page);
const newPages = [
...pages.slice(0, pageIndex),
currentPage,
...pages.slice(pageIndex + 1),
];
return { pages: newPages, pageParams: currentData.pageParams };
},
);
}
Example #3
Source File: useFeed.ts From apps with GNU Affero General Public License v3.0 | 6 votes |
findIndexOfPostInData = (
data: InfiniteData<FeedData>,
id: string,
): { pageIndex: number; index: number } => {
for (let pageIndex = 0; pageIndex < data.pages.length; pageIndex += 1) {
const page = data.pages[pageIndex];
for (let index = 0; index < page.page.edges.length; index += 1) {
const item = page.page.edges[index];
if (item.node.id === id) {
return { pageIndex, index };
}
}
}
return { pageIndex: -1, index: -1 };
}
Example #4
Source File: [id].tsx From next-crud with MIT License | 6 votes |
UserCreate: NextPage<IProps> = ({ user }) => {
const toast = useToast()
const { replace } = useRouter()
const queryClient = useQueryClient()
const onSubmit = async (values: IFormValues) => {
try {
const userData = await fetch(`/api/users/${user.id}`, {
method: 'PUT',
body: JSON.stringify(values),
headers: {
'Content-Type': 'application/json',
},
}).then((res) => res.json())
toast({
status: 'success',
description: 'User successfully updated',
duration: 2000,
})
replace('/users')
queryClient.setQueryData<
InfiniteData<TPaginationResult<User>> | undefined
>('users', (data) => {
const page = data?.pages.find((page) =>
page.data.some((userElem) => userElem.id === user.id)
)
if (page) {
const elemIdx = page.data.findIndex((data) => data.id === user.id)
page.data[elemIdx] = userData
}
return data
})
} catch (e) {
toast({
status: 'error',
description: 'Failed to update user',
duration: 2000,
})
}
}
return (
<Layout title={user.username} backRoute="/users">
<VStack spacing={4} width="100%">
<Heading>User edition</Heading>
<UserForm
initialValues={{ username: user.username }}
onSubmit={onSubmit}
/>
</VStack>
</Layout>
)
}