UseQuery +循环=?

时间:2020-02-29 04:55:31

标签: react-hooks react-apollo

如何循环使用useQuery。我知道,不建议每个钩子规则都将钩子放在循环/条件内。

场景:

const getSomething = (key) => {
  const [data] = useQuery(someQuery, {variables: key})
  return data;
}

const getInfo = () => {
  const keys = ["one", "two", "etc"];
  // ideally would like to loop over each key and get the data
  /* ideal code would be:
     return keys.map(key => getSomething(key));
  */
  // but since hooks can't be used in a loop, I have to end up writing like this:
  const data1 = getSomething(keys[0]);
  const data2 = getSomething(keys[1]);
  const data3 = getSomething(keys[2]);
  // data manipulation after... 

}

是否有更好的编写方法,而不必为每个键和数据进行硬编码?而且API必须使用单键查询。

0 个答案:

没有答案