我正在使用useLazyQuery
Apollo 的钩子并进入无限循环。这是我的组件:
import styled from 'styled-components'
import { useLazyQuery } from "@apollo/react-hooks"
import GENERATE_EFT_REFERENCE_NUMBER from "./graphql/generate-eft-reference-number.graphql"
let Link = styled.a`
color: ${props => props.theme.color.turquoise500};
cursor: pointer;
text-decoration: underline;
`
const GenerateEftReferenceLink = ({
externalAccountId,
financialMethodId,
internalAccountId,
highestReferenceNumber,
methodId,
updateMethod
}) => {
const [generateEftReference = {}, { called, loading, data }] = useLazyQuery(
GENERATE_EFT_REFERENCE_NUMBER,
{
variables: {
externalAccountId,
financialMethodId,
internalAccountId,
highestReferenceNumber
},
onCompleted: ({ generateEftReferenceNumber: reconciliationSerialNumber }) => {
updateMethod({ reconciliationSerialNumber }, methodId)
}
}
)
return <Link onClick={generateEftReference}>Click Me</Link>
}
export default GenerateEftReferenceLink
在onCompleted
选项中,我获取结果并使用回调 ( updateMethod
) 来更新我的对象。
我遇到的问题是查询被一遍又一遍地调用。我onCompleted
每次都点击它,它调用updateMethod
并且我停留在这个无限循环中。onClick
仅在单击时才调用(Link
我通过将 a 放入 来验证debugger
)onClick
,所以这一定与useLazyQuery
钩子有关。
我通过更改为使用ApolloConsumer
组件来解决此问题,但我想了解为什么我会使用钩子进入这种状态。
有任何想法吗?