Skip to main content

React Native

TanStack Query works seamlessly with React Native, providing the same powerful data synchronization features for mobile apps.

Installation

Install React Query in your React Native project:
React Query supports React Native 0.70+ and works with Expo, bare React Native, and React Native Web.

Setup

Configure React Query in your React Native app:
App.tsx

Basic Usage

Use React Query hooks just like in React web apps:
screens/PostsScreen.tsx

Pull-to-Refresh

Integrate with React Native’s pull-to-refresh:
Use isFetching instead of isLoading for the refresh indicator to show activity during background refetches.

Infinite Scroll

Implement infinite scrolling with useInfiniteQuery:

Mutations

Handle create, update, and delete operations:

App State Management

React Query automatically refetches when the app comes to foreground:
React Query’s focusManager integrates with React Native’s AppState to automatically refetch when users return to your app.

Network State Management

Handle online/offline states:
Install the required dependency:

Persistence

Persist query cache to AsyncStorage:
Be mindful of AsyncStorage size limits. Consider implementing cache size limits or selective persistence for production apps.

Optimistic Updates

Provide instant feedback in mobile apps:

React Navigation Integration

Prefetch data when navigating:

Error Boundaries

Handle errors gracefully in React Native:

Performance Tips

1

Optimize re-renders

Use notifyOnChangeProps to limit updates:
2

Implement pagination

Load data in chunks for better performance:
3

Configure appropriate cache times

Monitor query performance using React DevTools Profiler and React Query DevTools (web debugging).

Next Steps

TypeScript

Add type safety to your React Native queries

GraphQL

Use GraphQL in React Native apps