useView, useBalance, etc.) are intentionally thin - they manage loading and error states but don’t include caching, deduplication, or background refetching.
For production applications, we recommend pairing @near-kit/react with a data fetching library:
React Query
Most popular. Full-featured with devtools, mutations, and infinite queries.
SWR
Lightweight alternative from Vercel. Simpler API, smaller bundle.
React Query
TanStack Query (React Query) is the most popular data fetching library for React. It provides caching, background updates, stale-while-revalidate, and excellent DevTools.Installation
Setup
1
Add QueryClientProvider alongside NearProvider
2
Create custom hooks using useNear
Complete Example
Full Guestbook with React Query
Full Guestbook with React Query
Patterns
- Optimistic Updates
- Dependent Queries
- Parallel Queries
Update the UI immediately, then reconcile with the server response:
SWR
SWR is a lightweight data fetching library from Vercel. It’s simpler than React Query with a smaller bundle size.Installation
Setup
1
Add SWRConfig alongside NearProvider
2
Create custom hooks using useNear
Complete Example
Full Guestbook with SWR
Full Guestbook with SWR
Patterns
- Conditional Fetching
- Revalidation
- Global Mutate
Comparison
Recommendation: Use built-in hooks for prototypes and simple apps. For production, choose React Query for complex apps with mutations, or SWR for simpler read-heavy apps.
Common Pitfalls
Data not refreshing after mutations
Problem: You calluseCall to submit a transaction, then call refetch() from useView, but the data doesn’t update.
Cause: React state updates are batched and asynchronous. When you call refetch() immediately after a mutation, React may not have re-rendered yet, so the refetch uses stale parameters.
invalidateQueries() which properly handles the timing:
Queries not refetching when args change
Problem: You change the arguments touseView but it doesn’t refetch.
Cause: JavaScript object identity. If you create a new object each render, useView sees it as the same value (via JSON serialization), but if you’re computing args from state that hasn’t updated yet, you get stale data.
Solution: Ensure args are derived from state that has actually updated, or use React Query which handles this more predictably.