简体   繁体   English

如何根据应用程序的 state 有条件地将 DocumentNode 参数传递给 useLazyQuery 挂钩?

[英]How do I conditionally pass the DocumentNode parameter to a useLazyQuery hook depending on the state of my application?

I am trying to call different type of queries using the useLazyQuery hook depending on certain state changes in my application.我正在尝试根据应用程序中的某些 state 更改使用 useLazyQuery 挂钩调用不同类型的查询。 Is there a way to initialize useLazyQuery() without a DocumentNode and later pass the DocumentNode parameter and call the query?有没有办法在没有 DocumentNode 的情况下初始化 useLazyQuery() ,然后传递 DocumentNode 参数并调用查询?

To further explain here is my code explaining what I wish to achieve.在这里进一步解释是我的代码,解释了我希望实现的目标。

export const Interactions: React.FC<interactionsProps> = ({}) => {
  const [state, dispatch] = useReducer(InteractionListReducer, null);
  const { data: usersData } = useUsersQuery();
  const { data: filters } = useGet_FiltersQuery();

 const  [getInteractions,{data: interactionData, refetch: refetchInteraction, fetchMore}] = useLazyQuery()
// useLazyQuery REQUIRES A DOCUMENTNODE (QUERY AS PARAMETER) BUT I DO NOT KNOW THE TYPE OF QUERY YET
  
  useEffect(() => {
    if (state?.filterSelected?.id) {
      //THIS RETURNS THE DOCUMENT NODE BASED ON THE FILTER SELECTED
      const queryNode = getInteractionsQueryType(state.filterSelected.system); 


      //THEN HERE I WANT TO CALL THE HOOK WITH THE DOCUMENTNODE THAT I JUST OBTAINED 
       getInteractions({query: queryNode  })

    }
  }, [state?.filterSelected?.id]);

Is it possible to achieve this using useLazyQuery hook?是否可以使用 useLazyQuery 挂钩实现此目的? If not is there an alternative to achieve this kind of querying depending on the state of application?如果没有,是否有一种替代方法可以根据应用程序的 state 实现这种查询?

One of the rules of hooks is that the number of hooks run inside a component should not change, ie don't conditionally run a hook.钩子的规则之一是组件内部运行的钩子数量不应该改变,即不要有条件地运行一个钩子。

With that in mind:考虑到这一点:

  1. Don't render your component until there's a query to be run:在运行查询之前不要渲染你的组件:
{readyForInteractions ? <Interactions /> : null }
  1. Use whatever query you want in useLazyQuery , even one that varies based on stateuseLazyQuery中使用任何你想要的查询,甚至是基于 state 而变化的查询

And as a further optimization:作为进一步的优化:

  1. Combine your two queries into one, ex:将您的两个查询合并为一个,例如:
query interactions {
  useUsers {
    field1
    field2
  }
  useGetFilters {
    fieldA
    fieldB
  }
}

This will make dealing with loading and error states much simpler while also eliminating a redundant.network request.这将使处理loadingerror状态变得更加简单,同时也消除了冗余的网络请求。

  1. Finally, if your queries are only changing based on their parameters, use query variables to run them instead of injecting variables straight into the query text - as it appears you may be doing with getInteractionsQueryType(state.filterSelected.system)最后,如果您的查询仅根据其参数发生变化,请使用查询变量来运行它们,而不是将变量直接注入查询文本 - 看起来您可能正在使用getInteractionsQueryType(state.filterSelected.system)

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

相关问题 使用 useLazyQuery 挂钩调用查询时如何重新获取查询? - How do I refetchQueries when query is called using useLazyQuery hook? 如何在我的反应应用程序中使用 useState 钩子给出的 setState 方法增加 state object 的选定索引的计数 - How do I increment the count of a selected index of state object using setState method given by useState hook in my react application 如何根据 state 有条件地应用 onClick 侦听器 - How to conditionally apply an onClick listener depending on state 如何在 Redux 应用程序的 localstorage 中存储我的状态? - How do I store my state in localstorage in my Redux application? 如何将 state 传递到我的 action creator - How do I pass state into my action creator 如何将初始状态传递给我的减速器? - How do I pass initial state to my reducers? 如何传递钩子 state 值 - How to pass the hook state values 如何在组件之间传递 React Hook 模态状态? - How can I Pass React Hook Modal State Between Components? 如何使用 Nextjs 将我的 URL 与应用程序 state 同步 - How do i sync my URL with application state using Nextjs React Native:如何将应用程序 state 的答案作为道具而不是卡的内部 state 传递 - React Native: How do I pass the answer from application state as a prop and not the internal state of the card
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM