#how to skip queries based on an argument

3 messages · Page 1 of 1 (latest)

terse violet
#

what is the correct syntax? @obsidian stag

    const doesUserHaveActiveRelationship = useMiscStore(
        (state) => state.doesUserHaveActiveRelationship,
    );

    const {
        data: momentMagicRatioData,
        isPending: isPendingMomentMagicRatioData,
    } = useQueryWithStatus(
        api.insights.getCurrentMagicRatio,
        {
            type: "moment",
            perspective: "personal",
        },
        doesUserHaveActiveRelationship ? {} : "skip",
    );
obsidian stagBOT
#

Hi @terse violet. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The syntax you've used seems to be incorrect. In Convex, the useQuery hook accepts two arguments: the query function and the arguments for that function. If you want to skip the query based on a condition, you should pass "skip" as the second argument when the condition is met.

Here's the correct syntax based on the information provided in the knowledge sources:

const doesUserHaveActiveRelationship = useMiscStore(
    (state) => state.doesUserHaveActiveRelationship,
);

const {
    data: momentMagicRatioData,
    isPending: isPendingMomentMagicRatioData,
} = useQueryWithStatus(
    api.insights.getCurrentMagicRatio,
    doesUserHaveActiveRelationship ? {
        type: "moment",
        perspective: "personal",
    } : "skip"
);

In this code, if doesUserHaveActiveRelationship is true, the query arguments are passed to useQueryWithStatus. If doesUserHaveActiveRelationship is false, "skip" is passed, which means the query won't be run.

For more information, you can refer to the Convex documentation on skipping queries (https://docs.convex.dev/client/react#skipping-queries).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: