wagmi: bug: unnecessary query invalidations in `useWalletClient` & `useConnectorClient`
Describe the bug
vm! š
Weāve started using wagmi v2 in a bigger project and noticed that some hooks cause too many re-renders, which leads to poor application performance. Both @Yuripetusko and I tried to find the root of the issue earlier today, and turns out the problematic hooks are useWalletClient & possibly useConnectorClient. Both of these contain a useEffect like this:
useEffect(() => {
// invalidate when address changes
if (address) queryClient.invalidateQueries({ queryKey })
else queryClient.removeQueries({ queryKey }) // remove when account is disconnected
}, [address, queryClient])
This looks fine at first glance, but this logic runs every single time when a hook gets called for the first time inside a component, so essentially if we mount a component which contains the hook, itāll invalidate the query.
Link to Minimal Reproducible Example
https://codesandbox.io/p/sandbox/wagmi-use-wallet-client-2mfmgd
Steps To Reproduce
- Open the provided Codesandbox
- Connect your wallet
- Click on the āRe-render child componentā button (one or more times)
Youāll notice that the WalletClient UID changes every time the child componentās key changes.
Wagmi Version
2.5.11
Viem Version
2.8.14
TypeScript Version
5.4.2
Check existing issues
- I checked there isnāt already an issue for the bug I encountered.
Anything else?
We also tried to find a quick solution for this issue & figured I could post it here, maybe itāll serve as an inspiration for the final solution. So, I essentially stored the value of the account variable in a ref, and since refs donāt trigger re-renders, I could use it to compare the account with its previous value inside this useEffect:
const prevAddress = useRef<UseAccountReturnType<config>["address"]>(address)
useEffect(() => {
if (!address) {
// remove when account is disconnected
queryClient.removeQueries({ queryKey })
} else if (address !== prevAddress.current) {
// invalidate when address changes
queryClient.invalidateQueries({ queryKey })
}
prevAddress.current = address
}, [address, queryClient])
About this issue
- Original URL
- State: closed
- Created 3 months ago
- Comments: 15 (15 by maintainers)
Great work all around!
Go for it. I didnāt do much here, was just observing/helping out a bit, but you found a bug and found a fix, I just changed it slightly
ahh right š sorry. Long day
But if we follow the existing coding convention, then this is better
Although my last code perhaps has too much repetition. It can probably be shorter. I personally like verbosity in code, so itās easier to read and understand without spending too much time. Unlike @tmm and rest of wagmi guys where I noticed that it often looks like Coffeescript š i.e. not adding curly braces for conditions with 1 line. Thereās nothing bad with it, but I personally never allow our devs to do this lol.