useContractRead with args from other hooks (useState)
See original GitHub issueTrying to useContractRead
that has as an arg the address
from useAccount
like this:
const { address, isConnected } = useAccount();
const userContractBalance = useContractRead({
addressOrName: process.env.NEXT_PUBLIC_MY_CONTRACT_ADDRESS,
contractInterface: myABI,
functionName: 'balances',
args: [address]
});
console.log('userContractBalance is', address, userContractBalance.data);
However I notice that sometimes when I switch pages in my app and go back to this,
the userContractBalance.data
is undefined even though the address
is defined…
Sometimes it works sometimes not…
I then even tried adding this lower in my code:
useEffect(() => {
if (address) userContractBalance.refetch();
}, [address])
But it did not help. Not sure what the problem is. Any tips would be greatly appreciated.
Issue Analytics
- State:
- Created a year ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
How to pass dynamic args to useContractRead #449 - GitHub
I am using deployed contract, I am importing the below code and using it. const priceFeed = new ethers.Contract( PriceFeedDeployment.address, ...
Read more >React useEffect loop: circular dependency - Stack Overflow
I'm trying to call a function every time that function's parameters change and this has generated a loop that I don't know how...
Read more >Migration Guide - wagmi
args config option must now be an array for the following hooks: useContractRead , useContractWrite , usePrepareContractWrite , useContractReads , and ...
Read more >Wagmi useWaitForTransaction not waiting long enough?
I'm using wagmi react hooks to create a custom hook that handles token ... import {useState, useEffect} from 'react' import {utils} from ...
Read more >An End-to-End Tutorial to Create and Deploy a Fully ... - Ankr
So before we move ahead to next steps, let's add these variables in our ... Now let's deploy another contract with a sudo...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jellohouse would love to hear your solution. Struggling with this…
I am having the same issue.
I can’t pinpoint it, but it seems non-deterministic, which leads me to believe it’s useEffect that is causing it. I’m trying to call
useContractRead
in a useEffect hook and haveenabled
set to false due to it causing an infinite loop otherwise.I imagine it’s due to the address not being available in time (which I use as an argument).