Javascript useLazyQuery-重新呈现问题
要求: 我想在useffect中第一次自动调用查询,然后在函数中手动调用 问题:不变性冲突:重新渲染过多Javascript useLazyQuery-重新呈现问题,javascript,react-native,graphql,apollo,apollo-client,Javascript,React Native,Graphql,Apollo,Apollo Client,要求: 我想在useffect中第一次自动调用查询,然后在函数中手动调用 问题:不变性冲突:重新渲染过多 setState是导致此问题的原因,我假设。功能组件的“身体部位”在每次更改时都会运行,需要条件(或useffect挂钩)来阻止意外行为 只是不要对所有注释使用useState?你可以用f.e const allNewComments = (data && data.getPost) ? data.getPost.comments : []; 。。。如果要迭代所有新成员 如
setState
是导致此问题的原因,我假设。功能组件的“身体部位”在每次更改时都会运行,需要条件(或useffect
挂钩)来阻止意外行为
只是不要对所有注释使用useState
?你可以用f.e
const allNewComments = (data && data.getPost) ? data.getPost.comments : [];
。。。如果要迭代所有新成员
如果仍要使用useState
(与useLazyQuery
结合使用)。。。在onCompleted
option/handler中使用它,例如:
const [getComment, { loading, data }] = useLazyQuery(getCommentsQuery, {
variables: {
input: {
id: "5e5cb512e90bd40017385305",
},
},
onCompleted: (data) => {setAllNewComments( data.getPost.comments )}
const [getComment, { loading, data }] = useLazyQuery(getCommentsQuery, {
variables: {
input: {
id: "5e5cb512e90bd40017385305",
},
},
onCompleted: (data) => {setAllNewComments( data.getPost.comments )}