Configuring `useReadContract`
Configure the properties of the useReadContract
hook.
Configuring useReadContract
The useReadContract
hook has a number of configurable properties that will allow you to adapt it to your needs. You can combine the functionality of TanStack queries with useBlockNumber
to watch the blockchain for changes, although doing so will consume a number of API calls.
Objectives
By the end of this guide you should be able to:
- Use
useBlockNumber
and thequeryClient
to automatically fetch updates from the blockchain - Describe the costs of using the above, and methods to reduce those costs
- Configure arguments to be passed with a call to a
pure
orview
smart contract function - Call an instance of
useReadContract
on demand - Utilize
isLoading
andisFetching
to improve user experience
Fetching Updates from the Blockchain
You’ll continue with the project you’ve been building and last updated while learning about the useReadContract
hook.
Once the excitement of your accomplishment of finally reading from your own contract subsides, try using BaseScan to add another issue, or vote on an existing issue. You’ll notice that your frontend does not update. There are a few ways to handle this.
Fetch updates from the blockchain
Use useBlockNumber
with the watch
feature to automatically keep track of the block number, then use the queryClient
to update when that changes. Make sure you decompose the queryKey
from the return of useReadContract
.
Don’t do this, either use multi-call via useReadContracts
, or consolidate your view
s into a single function that fetches all the data you need in one call.
Pause on blur
Stop watching the blockchain if the website doesn’t have focus. Add a state variable to count how many times the function has settled, and one for if the page is focused. Set up event listeners to set the state of the latter when the page is focused or blurred.
Update the watch
for useBlockNumber
so that it only does so if pageIsFocused
.
Add a line to the useEffect
for blockNumber
and increment your counter as well.
Finally, surface your counter in the component.
Adjust the polling rate
Adjust your pollingInterval
by setting it in getDefaultConfig
in _app.tsx
:
Setting it to 30 seconds, or 30,000 milliseconds, will reduce your API calls dramatically, without negatively impacting members of the DAO.
Update on demand
Use a similar system to call your update function on demand. Add a button, a handler for that button, and a state variable for it to set:
Finally, set watch
to equal triggerRead
, instead of pageIsFocused
, and reset triggerRead
in the useEffect
.
Set UI elements based on status
Use the “is” return values to set UI elements depending on the status of the hook as it attempts to call a function on the blockchain.
You’ll probably see the button flicker very quickly since the call doesn’t take very long. For a production app, you’d need to add additional handling to smooth out the experience.
Pass arguments to useReadContract
Arguments are passed into a useReadContract
hook by adding an array of arguments, in order, to the args
property. Common practice is to use React state variables set by UI elements to enable the arguments to be set and modified. For example, you might create a drop-down to set issueNumber
, then fetch that issue with:
Conclusion
In this guide, you’ve learned how to use the watch
feature of useBlockNumber
combined with useEffect
and queryClient.invalidateQueries
to enable your frontend to see updates to your smart contract. You’ve also learned the costs of doing so, and some strategies for mitigation. You’ve learned how to pass arguments to your functions. Finally, you’ve learned how to use the properties returned by useReadContract
to adjust your UI to improve the experience for your users.