T
TanStack14mo ago
correct-apricot

Using useQueryClient inside onSuccess

Does anyone know if we can use the useQueryClient in the onSuccess function? The main reason is I want to use helper functions from other files. Card.query.ts
export const sendAddCard = () => {
const queryClient = useQueryClient()
return createMutation({
mutationKey: [MutationKeys.Folders],
mutationFn: (data: PostAddCardModel) => postAddCard(data),
onSuccess: (cards, data, q,) => {
// Use let cards = _getFolders() instead of:
let folders = [...queryClient.getQueryData([QueryKeys.Folders])]

// Also useQueryClient will give error if we use it in onSuccess function
},
})
};
export const sendAddCard = () => {
const queryClient = useQueryClient()
return createMutation({
mutationKey: [MutationKeys.Folders],
mutationFn: (data: PostAddCardModel) => postAddCard(data),
onSuccess: (cards, data, q,) => {
// Use let cards = _getFolders() instead of:
let folders = [...queryClient.getQueryData([QueryKeys.Folders])]

// Also useQueryClient will give error if we use it in onSuccess function
},
})
};
Folder.helper.ts
export function _getFolders() {
const queryClient = useQueryClient()
return queryClient.getQueryData([QueryKeys.Folders])
}
export function _getFolders() {
const queryClient = useQueryClient()
return queryClient.getQueryData([QueryKeys.Folders])
}
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?