[英]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:考虑到这一点:
{readyForInteractions ? <Interactions /> : null }
useLazyQuery
, even one that varies based on stateuseLazyQuery
中使用任何你想要的查询,甚至是基于 state 而变化的查询And as a further optimization:作为进一步的优化:
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.这将使处理
loading
和error
状态变得更加简单,同时也消除了冗余的网络请求。
getInteractionsQueryType(state.filterSelected.system)
getInteractionsQueryType(state.filterSelected.system)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.