Skip to main content
TanStack Query (React Query) integrates seamlessly with Tevm for fetching and caching Ethereum data in React applications.

Installation

Setup

Wrap your app with QueryClientProvider:

Basic Usage

Fetching Address Balance

Fetching Block Data

Transaction History

Advanced Patterns

Parallel Queries

Fetch multiple addresses simultaneously:

Dependent Queries

Chain queries when one depends on another:

Polling for Updates

Poll for block updates:

Optimistic Updates

Update cache optimistically when sending transactions:

Query Key Patterns

Structure query keys for efficient cache management:

Custom Hooks Library

Create a reusable hooks library:

Error Handling

Handle RPC errors gracefully:

React Suspense Integration

Use with React Suspense:

DevTools

Enable React Query DevTools for debugging:

Best Practices

Cache Configuration
  • Use staleTime: Infinity for immutable data (confirmed transactions, historical blocks)
  • Use short staleTime (5-15s) for frequently changing data (balances, pending state)
  • Use refetchInterval for data that changes on a known schedule (block numbers)
Rate LimitingPublic RPC endpoints have rate limits. Consider:
  • Batching requests where possible
  • Using staleTime to reduce refetches
  • Implementing request deduplication with query keys

Next Steps