T
tRPC

Optimizing Data Refresh with trpc in React/Nextjs? Is there a more efficient way?

Optimizing Data Refresh with trpc in React/Nextjs? Is there a more efficient way?

TTrongar9/20/2023
I'm working on a Nextjs project using trpc, and I've noticed that I'm repeating the same code for data refetching after mutations (add, update, delete) using trpc mutations. Is there a more efficient or DRY (Don't Repeat Yourself) approach to handle data refreshing after mutations with trpc in React? I'd appreciate any suggestions or best practices to optimize this process.
const addTask = trpcClient.tasks.addTask.useMutation({
onSettled: () => {
getTasks.refetch();
},
});
const setDone = trpcClient.tasks.setTaskStatus.useMutation({
onSettled: () => {
getTasks.refetch();
},
});

const deleteTask = trpcClient.tasks.deleteTask.useMutation({
onSettled: () => {
getTasks.refetch();
},
});
const addTask = trpcClient.tasks.addTask.useMutation({
onSettled: () => {
getTasks.refetch();
},
});
const setDone = trpcClient.tasks.setTaskStatus.useMutation({
onSettled: () => {
getTasks.refetch();
},
});

const deleteTask = trpcClient.tasks.deleteTask.useMutation({
onSettled: () => {
getTasks.refetch();
},
});
Jjthrilly9/21/2023
There's the thermonuclear option: https://trpc.io/docs/client/react/useContext#invalidate-full-cache-on-every-mutation But yeah, I'm also interested in this...
TTrongar9/21/2023
but how could it be, i don´t get it i want to make somthing like
const {addTask, deleteTask, setStatus} = trpcClient.tasks.(...).useMutation({
onSettled: () => {
getTasks.refetch();
},
});
const {addTask, deleteTask, setStatus} = trpcClient.tasks.(...).useMutation({
onSettled: () => {
getTasks.refetch();
},
});

Looking for more? Join the community!

T
tRPC

Optimizing Data Refresh with trpc in React/Nextjs? Is there a more efficient way?

Join Server