Open borremosch opened 4 years ago
Same issue here
My team is also seeing this issue. Causing way too many re-renders. We're going to try the same approach as mentioned by @borremosch.
EDIT: re-reading your original issue, I think my below is actually irrelevant, and the real reason is just that the setResult
is always going to be called, which will cause the re-renders: https://github.com/apollographql/apollo-client/blob/master/src/react/hooks/useSubscription.ts#L19-L23
@borremosch @filipmares I'm not 100% if this will solve your issues, but if you are defining your onSubscriptionData
handler inline, every time the hook is run/processed, you're actually passing an entirely new anonymous function to the hook.
The general way to resolve this is to use React's useCallback
handler:
Digging deeper to confirm the theory, I found the related code at the following:
public setOptions(
newOptions: CommonOptions<TOptions>,
storePrevious: boolean = false
) {
if (storePrevious && !equal(this.options, newOptions)) {
this.previousOptions = this.options;
}
this.options = newOptions;
}
In particular, the !equal(this.options, newOptions)
, which uses:
/**
* Performs a deep equality check on two JavaScript values, tolerating cycles.
*/
export function equal(a: any, b: any): boolean {
// ..snip..
}
I am seeing this happening when enabling reconnect.
A rerender is triggered every XX seconds (depends on timeout/keep-alive).
Any idea on how to prevent this rerender?
Today I faced the same issue with useSubscription
. Whenever a subscription is fired, a React component is re-rendered. It isn't mentioned in useSubscription
's API doc. What can I do to prevent the re-rendering?
@0xdevalias , could you elaborate your solution for me? It sounds good, but I'm not sure I follow. Thanks in advance!
My team is also seeing this issue. Causing way too many re-renders. We're going to try the same approach as mentioned by @borremosch.
Hi @filipmares , did you resolve this issue? If yes, could you tell me how to?
I am using
useSubscription
in one of my components. I do not use any of the return values, because I am using a complicated protocol in my subscription, and I do not want to act on every message I receive. Instead, I am using theonSubscriptionData
callback function. I was surprised to see that even though I only update state on some of the received messages, my component is rerendering continuously. It seems that usinguseSubscription
will trigger a re-render every time subscription data is received, even if it is discarded.It would be very useful to have a version of
useSubscription
that does not trigger a re-render whenever data is received, and instead let the user decide when to make state changes inonSubscriptionData
. For now, I have had to put my subscription in a sub-component, just to make the superfluous re-rendering computationally cheaper.I am using @apollo/react-hooks version
3.1.2
.