scatter
scatter
Explore posts from servers
TTCTheo's Typesafe Cult
Created by scatter on 9/26/2022 in #questions
Are tRPC endpoints awaited internally? Endpoint being called again before it finishes
I'm having this weird issue where I want to add a value to an array only if it doesn't already exist. I test this in my endpoint and return an error if so. But it's being called twice due to react's strict mode and in both cases the server hasn't seen the update yet.
// tRPC endpoint
console.log(arrayFromPrisma);
if (arrayFromPrisma.includes(input.value)) {
throw {message: "already included"};
}
await // prisma update array push...
// tRPC endpoint
console.log(arrayFromPrisma);
if (arrayFromPrisma.includes(input.value)) {
throw {message: "already included"};
}
await // prisma update array push...
this is called twice and prints out an empty array both times (and succeeds, and the value ends up in the db array twice). I'm awaiting everything I can. It must be starting one endpoint call before the other is finished, but why? I don't understand how to stop this. My mutate call is wrapped in a useEffect and I even invalidate the get query on cleanup. But it doesn't help because it's calling the endpoint too soon. I'm completely stuck.
9 replies
TTCTheo's Typesafe Cult
Created by scatter on 9/23/2022 in #questions
change query params without rerender
When you useMutation with trpc, you call the endpoint with some input when you call .mutate, which is easy enough to do when you click a button or something. Is it possible to do the same with a query? useQuery takes the input and I don't see a way to change that without rerendering and calling useQuery again - eg type something in a text box and use it for a query without rerendering. I know queries and mutations are pretty much the same under the hood so the solution is to just use a mutation instead but it feels semantically weird to the point that I feel like I'm missing something.
5 replies
TTCTheo's Typesafe Cult
Created by scatter on 9/18/2022 in #questions
nested push-or-create-array in zustand and immer
Is there an easier way to accomplish this with zustand and immer, ie avoiding the conditional? this is inside a zustand create with immer middleware.
// StateType = { knownPaths: { [key: string]: string[] }; };

addPath(region, location) {
set((state) => {
if (region in state.knownPaths) {
state.knownPaths[region].push(location);
} else {
state.knownPaths[region] = [location];
}
});
},
// StateType = { knownPaths: { [key: string]: string[] }; };

addPath(region, location) {
set((state) => {
if (region in state.knownPaths) {
state.knownPaths[region].push(location);
} else {
state.knownPaths[region] = [location];
}
});
},
16 replies