@@ -257,13 +257,17 @@ Base is a fast, low-cost Ethereum L2 built to bring the next billion users oncha
257257 ``` typescript components/IncrementButton.tsx lines expandable
258258 ' use client'
259259
260+ import { useEffect } from ' react'
260261 import {
261262 useWriteContract ,
262263 useWaitForTransactionReceipt ,
263264 useChainId ,
264265 useSwitchChain ,
265266 } from ' wagmi'
267+ import { readContractQueryOptions } from ' wagmi/query'
268+ import { useQueryClient } from ' @tanstack/react-query'
266269 import { baseSepolia } from ' wagmi/chains'
270+ import { config } from ' @/config/wagmi'
267271 import { COUNTER_ADDRESS , counterAbi } from ' @/config/counter'
268272
269273 export function IncrementButton() {
@@ -272,6 +276,20 @@ Base is a fast, low-cost Ethereum L2 built to bring the next billion users oncha
272276 const { data : hash, isPending, writeContract } = useWriteContract ()
273277 const { isLoading : isConfirming, isSuccess } =
274278 useWaitForTransactionReceipt ({ hash })
279+ const queryClient = useQueryClient ()
280+
281+ useEffect (() => {
282+ if (isSuccess ) {
283+ queryClient .invalidateQueries ({
284+ queryKey: readContractQueryOptions (config , {
285+ address: COUNTER_ADDRESS ,
286+ abi: counterAbi ,
287+ functionName: ' number' ,
288+ chainId: baseSepolia .id ,
289+ }).queryKey ,
290+ })
291+ }
292+ }, [isSuccess , queryClient ])
275293
276294 if (chainId !== baseSepolia .id ) {
277295 return (
@@ -311,6 +329,10 @@ Base is a fast, low-cost Ethereum L2 built to bring the next billion users oncha
311329 }
312330 ```
313331
332+ <Note >
333+ ` useReadContract ` caches its result and does not automatically refetch after a write. Use ` queryClient.invalidateQueries ` with the read's query key to trigger a single refetch when a transaction confirms.
334+ </Note >
335+
314336 Surface three states to the user: waiting for wallet signature, waiting for on-chain confirmation, and success.
315337
316338 <Warning >
@@ -355,6 +377,7 @@ Base is a fast, low-cost Ethereum L2 built to bring the next billion users oncha
355377 ``` typescript components/BatchIncrement.tsx lines expandable
356378 ' use client'
357379
380+ import { useEffect } from ' react'
358381 import {
359382 useSendCalls ,
360383 useWaitForCallsStatus ,
@@ -364,11 +387,21 @@ Base is a fast, low-cost Ethereum L2 built to bring the next billion users oncha
364387 useChainId ,
365388 useSwitchChain ,
366389 } from ' wagmi'
390+ import { readContractQueryOptions } from ' wagmi/query'
391+ import { useQueryClient } from ' @tanstack/react-query'
367392 import { encodeFunctionData } from ' viem'
368393 import { baseSepolia } from ' wagmi/chains'
394+ import { config } from ' @/config/wagmi'
369395 import { useWalletCapabilities } from ' @/hooks/useWalletCapabilities'
370396 import { COUNTER_ADDRESS , counterAbi } from ' @/config/counter'
371397
398+ const counterQueryKey = readContractQueryOptions (config , {
399+ address: COUNTER_ADDRESS ,
400+ abi: counterAbi ,
401+ functionName: ' number' ,
402+ chainId: baseSepolia .id ,
403+ }).queryKey
404+
372405 export function BatchIncrement() {
373406 const { isConnected } = useAccount ()
374407 const { supportsBatching } = useWalletCapabilities ()
@@ -385,6 +418,13 @@ Base is a fast, low-cost Ethereum L2 built to bring the next billion users oncha
385418 const { isLoading : isConfirming, isSuccess } = useWaitForCallsStatus ({
386419 id: data ?.id ,
387420 })
421+ const queryClient = useQueryClient ()
422+
423+ useEffect (() => {
424+ if (isSuccess ) {
425+ queryClient .invalidateQueries ({ queryKey: counterQueryKey })
426+ }
427+ }, [isSuccess , queryClient ])
388428
389429 if (chainId !== baseSepolia .id ) {
390430 return (
@@ -428,7 +468,15 @@ Base is a fast, low-cost Ethereum L2 built to bring the next billion users oncha
428468 const chainId = useChainId ()
429469 const { switchChain, isPending : isSwitching } = useSwitchChain ()
430470 const { data : hash, isPending, writeContract } = useWriteContract ()
431- const { isLoading : isConfirming } = useWaitForTransactionReceipt ({ hash })
471+ const { isLoading : isConfirming, isSuccess } =
472+ useWaitForTransactionReceipt ({ hash })
473+ const queryClient = useQueryClient ()
474+
475+ useEffect (() => {
476+ if (isSuccess ) {
477+ queryClient .invalidateQueries ({ queryKey: counterQueryKey })
478+ }
479+ }, [isSuccess , queryClient ])
432480
433481 if (chainId !== baseSepolia .id ) {
434482 return (
0 commit comments