How do I stop a trpc query from running when a parameter is null?

In this example, since the TRPC procedure only accepts productId as a string that's not nullable, I still get a type error for productId.
export const useStockByProductId = (productId: string | null) => {
return api.stock.getStockByProductId.useQuery(
{
productId,
},
{
enabled: productId !== null,
}
);
};
export const useStockByProductId = (productId: string | null) => {
return api.stock.getStockByProductId.useQuery(
{
productId,
},
{
enabled: productId !== null,
}
);
};
Do I just update this to
export const useStockByProductId = (productId: string | null) => {
return api.stock.getStockByProductId.useQuery(
{
productId: productId ?? "", // Seems like I'm hacking around TypeScript...
},
{
enabled: productId !== null,
}
);
};
export const useStockByProductId = (productId: string | null) => {
return api.stock.getStockByProductId.useQuery(
{
productId: productId ?? "", // Seems like I'm hacking around TypeScript...
},
{
enabled: productId !== null,
}
);
};
3 Replies
DYELbrah
DYELbrah12mo ago
Current type error
Brendonovich
Brendonovich12mo ago
you can't really, if you've got the enabled there the best u can do is to use ! to force a non-null value
DYELbrah
DYELbrah12mo ago
Oh dang Interesting Thanks again Brendonovich!
Want results from more Discord servers?
Add your server
More Posts
Should I use setState inside useEffect with React Query (or TRPC)?Lets say I want to fetch a product and set the result to a zustand store. ``` //Get zustand store seany typescript genius can help with writing a wrapper function for a SvelteKit loader ?the way that the auth library i'm working with works, is that it recommends reading in the auth fromArchiving on serverless functionsDoes anyone know any zipper/achiver library that works on serverless functions?Admin dashboardHow could I create an admin dashboard where if the user is not authenticated with next-auth it goes Clerk - can’t get types of unsafeMetadata properties on user object?I have some unsafeMetadata stored on the user object for users in my t3-app. When I try to access anhow to get rid of t3 icon in Head component?I mean, it's a good icon, but I'd like to have my own in place. I have <link rel='icon' href='/my-iMutation alters user in database then redirects to homepage. How can i force user session to updateI have a page /form when i press the submit button i activate a mutation that updates a field on useStoring a Triple Nested ObjectSo my prisma schema is as follows: ```prisma model Game { id Int @id @default(autoincrement()) Cold starts on Edge?Hi, I have an API route that I made using Planetscale's database-js library. Here's the code: ``` imYear old IntelliJ bug is actively making my development experience miserableIntelliJ has had some type of CPU bug while trying to parse mildly complex Typescript types for abou