Integrating TanStack Query into a React Native Micro App with Repack

June 18, 2024 (5mo ago)

This guide provides detailed instructions for integrating @tanstack/react-query into a React Native micro app using Repack. By leveraging Webpack through Repack, you gain access to advanced bundling techniques while ensuring compatibility with the CommonJS (CJS) format, which works more reliably within the React Native ecosystem.


Prerequisites

Ensure that you have:


Step 1: Install TanStack Query

Install @tanstack/react-query using npm or yarn:

npm install @tanstack/react-query

This installs TanStack Query, providing powerful utilities for managing data fetching and caching within your micro app. This guide will focus on using the CommonJS (CJS) version to ensure compatibility in the React Native environment.


Step 2: Configure Webpack with Repack for CJS Compatibility

Configure Webpack to prioritize CommonJS (CJS) modules for TanStack Query. This approach ensures better compatibility in React Native by using a CJS-based setup.

Open your Webpack configuration file (usually webpack.config.mjs) and configure the module handling, especially for CJS.

You can find an example Webpack configuration file here.

Key Configuration Details

  1. CJS Compatibility for @tanstack/react-query:

    • Ensure @tanstack files are processed correctly by specifying their paths in the Babel loader configuration.
    • The configuration includes the use of @babel/plugin-transform-private-methods to support private methods within @tanstack, a requirement for compatibility in environments that do not natively support certain JavaScript features.
  2. Babel Configuration for React Native and TanStack:

    • Includes babel-loader to transpile JavaScript and TypeScript files, ensuring that React Native and @tanstack/react-query dependencies are compatible with React Native.
    • The include paths ensure that @tanstack dependencies are processed correctly, avoiding runtime issues.

Step 3: Create a Query Client

Each micro app can have its own QueryClient instance, allowing each app to manage data caching and request handling independently. This modular approach prevents unintended data sharing and keeps micro apps self-contained.

You can view an example implementation of the App component here.

Example:

import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
 
const queryClient = new QueryClient();
 
export const App = () => (
  <QueryClientProvider client={queryClient}>
    {/* Your App Components */}
  </QueryClientProvider>
);

Step 4: Use TanStack Query Hooks in Your Components

With your QueryClient set up, you can now use TanStack Query’s hooks (like useQuery and useMutation) to fetch and manage data in your components.

You can find a complete implementation of DataComponent using TanStack Query here.

Example Usage of useQuery:

import { useQuery } from '@tanstack/react-query';
import { Text } from 'react-native';
 
const fetchData = async () => {
  const response = await fetch('https://api.example.com/data');
  return response.json();
};
 
const DataComponent = () => {
  const { data, error, isLoading } = useQuery(['dataKey'], fetchData);
 
  if (isLoading) return <Text>Loading...</Text>;
  if (error) return <Text>Error loading data</Text>;
 
  return <Text>{JSON.stringify(data)}</Text>;
};

Step 5: Troubleshooting and Best Practices

  1. Prioritize CJS for Compatibility:

    • Ensure the Webpack configuration processes TanStack Query as CommonJS to avoid compatibility issues in React Native.
    • Avoid mixing ESM and CJS formats within the same setup to prevent runtime errors.
  2. Private Method Transform:

    • Include the @babel/plugin-transform-private-methods plugin in Babel’s configuration to ensure @tanstack libraries work correctly, as some internal methods may rely on private fields and methods.
  3. React Refresh for HMR (Optional):

    • If using Hot Module Replacement (HMR) during development, ensure react-refresh/babel is configured within babel-loader to see live updates without a full refresh.

Conclusion

By following this guide, you have successfully integrated TanStack Query into your React Native micro app using Repack with an emphasis on CommonJS (CJS) compatibility. Each micro app manages its own QueryClient, ensuring modularity and self-containment without shared instances. This setup maximizes compatibility within the React Native ecosystem and avoids issues related to ESM/CJS interop, making it a stable foundation for data fetching and caching in microfrontends.

This approach is particularly suited for modular architectures, where self-contained micro apps need isolated state and minimal dependencies on shared modules.