简体   繁体   English

如何使用打字稿有条件地将useQuery放入react-query中

[英]how to put useQuery in react-query in conditionally using typescript

I am using useQuery from react-query to fetch data that I only want the query to run in some condition.我正在使用react-query useQuery来获取我只希望查询在某些条件下运行的数据。 How can I use it?我怎样才能使用它? following is my code to use useQuery以下是我使用useQuery代码

  const query = useQuery<APIResponse, Error>(
    [{query: creatGQL, variables: variables}],
    async () => {
      const result: APIResponse = await ucFetch(apiUrl, {
        method: 'POST',
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({
          query: creatGQL,
          variables: variables,
        }),
      });
      return result;
    }  );
  return query;

A query can be disabled / enabled by using the enabled option.可以使用enabled选项禁用/启用查询。 If it is false , the query will not run:如果它是false ,查询将不会运行:

useQuery(key, queryFn, { enabled: myCondition }

the condition can be static, or driven by state / props - anything that's a boolean really.条件可以是静态的,也可以是由状态/道具驱动的 - 任何真正的布尔值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM