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:
- Node.js and npm/yarn installed.
- A React Native project set up to use Repack for Webpack-based bundling.
- Basic familiarity with Webpack configurations and Repack.
Step 1: Install TanStack Query
Install @tanstack/react-query
using npm or yarn:
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
-
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.
- Ensure
-
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.
- Includes
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:
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
:
Step 5: Troubleshooting and Best Practices
-
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.
-
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.
- Include the
-
React Refresh for HMR (Optional):
- If using Hot Module Replacement (HMR) during development, ensure
react-refresh/babel
is configured withinbabel-loader
to see live updates without a full refresh.
- If using Hot Module Replacement (HMR) during development, ensure
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.