question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useContractRead with args from other hooks (useState)

See original GitHub issue

Trying 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:closed
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
0xcertycommented, Nov 6, 2022

@jellohouse would love to hear your solution. Struggling with this…

1reaction
0xcertycommented, Nov 6, 2022

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 have enabled 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).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found