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
DYELbrahOP2y ago
Current type error
Brendonovich
Brendonovich2y 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
DYELbrahOP2y ago
Oh dang Interesting Thanks again Brendonovich!
Want results from more Discord servers?
Add your server