Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve useReadContract example or TanStack Query docs #4344

Closed
brianonchain opened this issue Oct 16, 2024 · 2 comments
Closed

Improve useReadContract example or TanStack Query docs #4344

brianonchain opened this issue Oct 16, 2024 · 2 comments
Labels
Area: Docs Area: Documentation

Comments

@brianonchain
Copy link

brianonchain commented Oct 16, 2024

What is the type of issue?

Documentation is missing

What is the issue?

The most basic example of useReadContract is to read a ERC20 token balance. Required args include the account address and, as most protocols are multichain, the chain.id or chain.name. Both variables are fetched using another hook (useAccount). Therefore, useReadContract is a "dependent query" (https://tanstack.com/query/latest/docs/framework/react/guides/dependent-queries) and should use the "enabled" key: query: {enabled: address && chain? true : false}.

Most Wagmi developers may not be super experienced with React Query, so this is essential in my opinion. Or, at the very least, include it in the "Tanstack Query Guide" in the docs.

So, the updated example can be something like:

export default function Vaults() {
  const { chain, address } = useAccount();

  const { data: balance } = useReadContract({
    address: usdcAddress[chain?.name!]  // usdcAddress is a dictionary of chain names to addresses
    abi: abi,
    functionName: "balanceOf",
    args: [address!],
    query: {enabled: address && chain?.name ? true : false}
  })
}

Where did you find it?

https://wagmi.sh/react/guides/read-from-contract
or
https://wagmi.sh/react/guides/tanstack-query

@brianonchain brianonchain added the Area: Docs Area: Documentation label Oct 16, 2024
@Zoiemaediez
Copy link

Uploading 1000000073.png…

@tmm tmm closed this as completed in 254f8f7 Nov 5, 2024
Copy link
Contributor

This issue has been locked since it has been closed for more than 14 days.

If you found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Wagmi version. If you have any questions or comments you can create a new discussion thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Area: Docs Area: Documentation
Projects
None yet
Development

No branches or pull requests

3 participants
@brianonchain @Zoiemaediez and others