-
Notifications
You must be signed in to change notification settings - Fork 787
useLazyQuery onCompleted called on every re-render #3505
Comments
The same happen for me.
But if I execute a function returned by
Version
|
@hwillson Could you take a look? |
@vilvaathibanpb I took a look and figured the problem is here: https://github.com/apollographql/react-apollo/blob/master/packages/hooks/src/utils/useBaseQuery.ts#L57. I didn't start working on fix tho, so go ahead. |
@bsonntag Sure, Let me raise a PR against it. Thanks |
Hey @bsonntag , I was checking the issue and my observations are:
Do you still think, we should work on fixing the I sense that, the |
My understanding is that Right now For example, the following component will enter an infinite re-render loop when the query completes: function Users() {
const [count, setCount] = useState(0);
const [fetchUsers, { data, loading }] = useLazyQuery(usersQuery, {
onCompleted: () => setCount(count => count + 1)
});
return (
<>
<button onClick={() => fetchUsers()}>Fetch</button>
{/* Render users */}
</>
);
} |
@bsonntag Yes. I completely get the issue. But when I debugged the issue, I dont think it is enough if we just change the May be this |
Sure. lets wait for him to respond |
Any update on this issue? |
Any updates? |
Any progress or possible work around? |
It seems the apollo team is disinterested in this so don't expect a solution any time soon. |
I found a solution but to implement it, I need clarity on other code change which was merged earlier from a core member. But I am still waiting for response |
Hi all - sorry for the delay here. We're heads down working on Apollo Client 3, which is going to supersede the React Apollo project (React Hooks functionality will be available from AC 3 directly). This unfortunately means we haven't had a chance to keep an eye on issues as much as we'd like 🙁. That being said, if I revert #3497 and publish a new RA version today, would that be a good stop gap for now? |
Would be nice, thanks. |
Rolled back and deployed in 3.1.3 - thanks! |
What about this? It keeps happening to me, what can I do for now? |
As a workaround, I've found a use of const [skipQuery, setSkipQuery] = useState(true);
let { loading } = useQuery(MyQuery, {
client: myClient,
variables: { var1, var2 },
skip: skipQuery,
onCompleted: data => {
setData(data);
setSkipQuery(true);
},
});
useEffect(() => {
setSkipQuery(false);
return () => setSkipQuery(true);
}, [props.dep1, props.dep2]); I hope that helps. |
this issue still happens in version 3.1.3 |
@hwillson Ran into this on 3.1.3 today. |
@MarkPolivchuk @futantan Please open a new issue with a runnable reproduction if you're still having trouble with this. Also maybe take a look at |
My API is not calling 2nd time when I am setting the compareItems value [];
|
I am having the same issue and the above solutions doesn't help my case |
Any fix for this yet? This keeps happening to us with @apollo/client v3 beta as well. |
Not sure where I'm doing something wrong. I want to call the query the 1st time automatically in |
@dhavaljardosh How about this:
|
I absolutely agree with @chattling. A bit addition from my point.
|
This seems to still be happening in 3.1.5 if I do useState and useLazyQuery together.
|
@Phoenix1405, the working design pattern would be to set state inside onCompleted event and you don't need to destructure loading and data, see example above. |
keep a console.log inside the useEffect and onCompleted. Now observe whether the useEffect is firing multiple time or OnComplete. If onCompleted log firing multiple times then the issue is somewhere else. |
when the variables are same in
so for this try fetchPolicy: 'network-only'
|
This worked for me as a workaround:
|
run into a similar issue where
|
Try my above solution, I was having the same issue with useLazyQuery. Or simply check if there is data in a useEffect. Currently the if statement will get called on every re-render, where a useEffect with data as a dependency will only update when data is updated. |
@LucasClaude hey thanks yeah that trick works for me too. it just seems wrong to have to use a extra flag and useEffects. seems the whole premise of useLazyQuery would be to use it with a button. how does it work for anyone else? the example doesn't have any workarounds like this so seems like a design flaw? or i am misunderstanding hooks :) |
UseEffect will be called for any state change. So you just have to know first what is your requirements. Generally useLazyQuery use for late loading purpose. |
Intended outcome:
useLazyQuery only calls onCompleted on a successful network request
Actual outcome:
useLazyQuery calls onCompleted after every re-render even if the result is being taken from the cache
Version
npmPackages:
apollo-cache-inmemory: ^1.6.3 => 1.6.3
apollo-client: ^2.6.4 => 2.6.4
apollo-link-context: ^1.0.19 => 1.0.19
apollo-link-http: ^1.5.16 => 1.5.16
react-apollo: ^3.1.1 => 3.1.1
The text was updated successfully, but these errors were encountered: