我使用graphql query
呼叫了多个useQuery
。它显示了类似的错误
Too many re-renders. React limits the number of renders to prevent an infinite loop.
我知道为什么会发生,但我不知道如何防止出现此错误。这是Functional components
我的代码在这里
const [categoryList, updateCategoryList] = useState([]);
const [payeeList, updatePayeeList] = useState([]);
if (data) {
const categories = (data.categories as unknown) as Category[];
if (
!isEqual(categoryList, categories) ||
categoryList.length !== categories.length
) {
updateCategoryList([...categories]);
}
}
if (isEmpty(payeeList)) {
const { loading, data } = useQuery(payeesQuery);
if (data) {
const payees = (data.payees as unknown) as Payee[];
if (!isEqual(payeeList, payees) || payeeList.length !== payees.length) {
updateCategoryList([...payees]);
}
}
}
抱歉,我没有反应。
答案 0 :(得分:1)
例如,您可以这样使用
const ExampleComponent = () => {
const { loading:payeesLoading, data:payeesData } = useQuery(payeesQuery);
const { loading:secondLoading, data:secondData } = useQuery(secondQuery);
useEffect(() => { //effect hook for first query
} ,[payeesData] )
useEffect(() => { //effect hook for second query
} ,[secondData] )
return (<div>
your contents
</div>)
}
像这样,您可以在单个组件中编写多个查询。