T
TanStack14mo ago
sunny-green

refetching isnt working

const userHoldings = useQuery({
queryKey: ['userHoldings', searchParams.get('selected')],
queryFn: () => getUserHoldings(searchParams.get('selected') as string),
})
const query = useQuery({
queryKey: [
'usersWithHoldings',
searchParams.get('users'),
searchParams.get('search'),
],
queryFn: () =>
getHoldings(
searchParams.get('users') || '',
searchParams.get('search') || ''
),
})


// THOSE ABOVE ARE THE QUERIES THAT I GET DATA FROM


const handleItemAssigner = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault()
if (!assetId) return
const payload = {
userId: searchParams.get('selected'),
takenDate: new Date().toISOString(),
}
const res = await AxiosInstance.patch(`/asset/${assetId}`, payload)
if ([200, 201].includes(res.status)) {
// THIS DOES ABSOLUTELY NOTHING
queryClient.invalidateQueries({
queryKey: ['usersWithHoldings', 'userHoldings'],
})
} else {
alert('Something went wrong')
}
}
const userHoldings = useQuery({
queryKey: ['userHoldings', searchParams.get('selected')],
queryFn: () => getUserHoldings(searchParams.get('selected') as string),
})
const query = useQuery({
queryKey: [
'usersWithHoldings',
searchParams.get('users'),
searchParams.get('search'),
],
queryFn: () =>
getHoldings(
searchParams.get('users') || '',
searchParams.get('search') || ''
),
})


// THOSE ABOVE ARE THE QUERIES THAT I GET DATA FROM


const handleItemAssigner = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault()
if (!assetId) return
const payload = {
userId: searchParams.get('selected'),
takenDate: new Date().toISOString(),
}
const res = await AxiosInstance.patch(`/asset/${assetId}`, payload)
if ([200, 201].includes(res.status)) {
// THIS DOES ABSOLUTELY NOTHING
queryClient.invalidateQueries({
queryKey: ['usersWithHoldings', 'userHoldings'],
})
} else {
alert('Something went wrong')
}
}
It doesnt work with useMutation also, where is my mistake, what im doing wrong
3 Replies
other-emerald
other-emerald14mo ago
Try separating these out per cache key. queryClient.invalidateQueries({ queryKey: ['usersWithHoldings']}) queryClient.invalidateQueries({ queryKey: ['userHoldings']}) The queryKey array is meant to be a combination, not seperate cache values.
sunny-green
sunny-greenOP14mo ago
I've done that too and it isn't working
correct-apricot
correct-apricot14mo ago
Where is queryClient from? useQueryClient?

Did you find this page helpful?