tRPC, query invalidation is not working

This is the code I have inside the component const utils = trpc.useContext() const students = trpc.student.getAll.useQuery() const addStudentTRPC = trpc.student.add.useMutation({ onSuccess() { utils.student.getAll.invalidate() }, }) The mutation works fine but it doesn't invalidate the queries on success. I tried fetch(), refetch() and they don't work either. What am I missing? Thanks!
9 Replies
Neto
Neto•3y ago
have you awaited?
theFern
theFernOP•3y ago
Yes, still doesn't work :/ const utils = trpc.useContext() const students = trpc.student.getAll.useQuery() const addStudentTRPC = trpc.student.add.useMutation({ onSuccess() { utils.student.getAll.invalidate() }, }) const handleAddStudent = async (values: any) => { await addStudentTRPC.mutate({ studentFirstName: values.studentFirstName, studentLastName: values.studentLastName, studentDateOfBirth: values.studentDateOfBirth, userId: values.teacher, }) }
Neto
Neto•3y ago
you can add the devtools and check what is going on if the query is at least being called
Neto
Neto•3y ago
Devtools | TanStack Query Docs
Wave your hands in the air and shout hooray because React Query comes with dedicated devtools! 🥳 When you begin your React Query journey, you'll want these devtools by your side. They help visualize all of the inner workings of React Query and will likely save you hours of debugging if you find yourself in a pinch!
theFern
theFernOP•3y ago
Yes, the query is being fetched. Oddly enough, it always shows as "Stale"
Neto
Neto•3y ago
its the correct behaviour rq by default have some aggressive caching you can trigger the mutation and check if the query is being invalidated and refetched
theFern
theFernOP•3y ago
It looks like the invalidate() is being called
Neto
Neto•3y ago
Probably you just need to wait then
danmrkw
danmrkw•2y ago
thanks for this! read it, checked my code, facepalmed 🙂
Want results from more Discord servers?
Add your server