Skip to content

Commit cbc2704

Browse files
committed
docs: up
1 parent 466f4e8 commit cbc2704

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

site/react/guides/read-from-contract.md

+54
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,60 @@ function ReadContract() {
7070
}
7171
```
7272

73+
## Refetching On Blocks
74+
75+
The [`useBlockNumber` Hook](/react/api/hooks/useBlockNumber) can be utilized to refetch or [invalidate](https://tanstack.com/query/latest/docs/framework/react/guides/query-invalidation) the contract data on a specific block interval.
76+
77+
:::code-group
78+
```tsx [read-contract.tsx (refetch)]
79+
import { useEffect } from 'react'
80+
import { useBlockNumber, useReadContract } from 'wagmi'
81+
82+
function ReadContract() {
83+
const { data: balance, refetch } = useReadContract({
84+
...wagmiContractConfig,
85+
functionName: 'balanceOf',
86+
args: ['0x03A71968491d55603FFe1b11A9e23eF013f75bCF'],
87+
})
88+
const { data: blockNumber } = useBlockNumber({ watch: true })
89+
90+
useEffect(() => {
91+
// want to refetch every `n` block instead? use the modulo operator!
92+
// if (blockNumber % 5 === 0) refetch() // refetch every 5 blocks
93+
refetch()
94+
}, [blockNumber])
95+
96+
return (
97+
<div>Balance: {balance?.toString()}</div>
98+
)
99+
}
100+
```
101+
```tsx [read-contract.tsx (invalidate)]
102+
import { useQueryClient } from '@tanstack/react-query'
103+
import { useEffect } from 'react'
104+
import { useBlockNumber, useReadContract } from 'wagmi'
105+
106+
function ReadContract() {
107+
const queryClient = useQueryClient()
108+
const { data: balance, refetch } = useReadContract({
109+
...wagmiContractConfig,
110+
functionName: 'balanceOf',
111+
args: ['0x03A71968491d55603FFe1b11A9e23eF013f75bCF'],
112+
})
113+
const { data: blockNumber } = useBlockNumber({ watch: true })
114+
115+
useEffect(() => {
116+
// if `useReadContract` is in a different hook/component,
117+
// you can import `readContractQueryKey` from `'wagmi/query'` and
118+
// construct a one-off query key to use for invalidation
119+
queryClient.invalidateQueries({ queryKey })
120+
}, [blockNumber, queryClient])
121+
122+
return (
123+
<div>Balance: {balance?.toString()}</div>
124+
)
125+
}
126+
```
73127
:::
74128

75129
## Calling Multiple Functions

site/vue/guides/read-from-contract.md

+56
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,62 @@ const {
7575

7676
:::
7777

78+
<!-- TODO: ## Refetching On Blocks
79+
80+
The [`useBlockNumber` Hook](/react/api/hooks/useBlockNumber) can be utilized to refetch or [invalidate](https://tanstack.com/query/latest/docs/framework/react/guides/query-invalidation) the contract data on a specific block interval.
81+
82+
:::code-group
83+
```tsx [read-contract.tsx (refetch)]
84+
import { useEffect } from 'react'
85+
import { useBlockNumber, useReadContract } from 'wagmi'
86+
87+
function ReadContract() {
88+
const { data: balance, refetch } = useReadContract({
89+
...wagmiContractConfig,
90+
functionName: 'balanceOf',
91+
args: ['0x03A71968491d55603FFe1b11A9e23eF013f75bCF'],
92+
})
93+
const { data: blockNumber } = useBlockNumber({ watch: true })
94+
95+
useEffect(() => {
96+
// want to refetch every `n` block instead? use the modulo operator!
97+
// if (blockNumber % 5 === 0) refetch() // refetch every 5 blocks
98+
refetch()
99+
}, [blockNumber])
100+
101+
return (
102+
<div>Balance: {balance?.toString()}</div>
103+
)
104+
}
105+
```
106+
```tsx [read-contract.tsx (invalidate)]
107+
import { useQueryClient } from '@tanstack/react-query'
108+
import { useEffect } from 'react'
109+
import { useBlockNumber, useReadContract } from 'wagmi'
110+
111+
function ReadContract() {
112+
const queryClient = useQueryClient()
113+
const { data: balance, refetch } = useReadContract({
114+
...wagmiContractConfig,
115+
functionName: 'balanceOf',
116+
args: ['0x03A71968491d55603FFe1b11A9e23eF013f75bCF'],
117+
})
118+
const { data: blockNumber } = useBlockNumber({ watch: true })
119+
120+
useEffect(() => {
121+
// if `useReadContract` is in a different hook/component,
122+
// you can import `readContractQueryKey` from `'wagmi/query'` and
123+
// construct a one-off query key to use for invalidation
124+
queryClient.invalidateQueries({ queryKey })
125+
}, [blockNumber, queryClient])
126+
127+
return (
128+
<div>Balance: {balance?.toString()}</div>
129+
)
130+
}
131+
```
132+
::: -->
133+
78134
<!-- TODO: ## Calling Multiple Functions
79135
80136
We can use the [`useReadContract` Hook](/react/api/hooks/useReadContract) multiple times in a single component to call multiple functions on the same contract, but this ends up being hard to manage as the number of functions increases, especially when we also want to deal with loading & error states.

0 commit comments

Comments
 (0)