[英]how to call conditional useQuery hook in react/apollo
So I know I can use skip
on query to skip them, but, this require me to do something like this所以我知道我可以使用
skip
查询来跳过它们,但是,这需要我做这样的事情
const [fetchQuery1, { loading1, data1 }] = useQuery({
//..
skip: cond1
});
const [fetchQuery2, { loading2, data2 }] = useQuery({
//..
skip: cond2
});
const [fetchQuery3, { loading3, data3 }] = useQuery({
//..
skip: cond3
});
but this is a lot of variable for some unused query但对于一些未使用的查询来说,这是很多变量
I have a code, where basiaclly there is 3 query, and only one will fire out of the three.我有一个代码,其中基本上有 3 个查询,并且三个查询中只有一个会触发。 Instead of creating a milions variable than add a
if(loading1 || loading2 || loading3)
I would like to have a single loading variable.而不是创建一个 milions 变量而不是添加一个
if(loading1 || loading2 || loading3)
我想要一个单一的加载变量。
so right now I was doing所以现在我正在做
const { data, loading, error } = cond1
? useQuery(Query1)
: cond2 ? useQuery(Query2) : useQuery(Query3)
so the above works, but it goes against the rule of conditional hooks.所以上述工作,但它违反了条件挂钩的规则。
So I wonder if there is a nice way to do this, without polluting the variables所以我想知道是否有一个很好的方法来做到这一点,而不污染变量
You could conditionally pass the gql document to the useQuery hook.您可以有条件地将 gql 文档传递给 useQuery 挂钩。
const QUERY = cond1 ? QUERY1 : cond2 ? QUERY2 : QUERY3;
const { data, loading, error } = useQuery(QUERY);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.