React Native
React Native is the best way to run Triplit on a mobile app. The hooks available in the React package are also available in React Native.
Expo
If you are using Expo to setup your React Native project, you can follow these steps to get Triplit up and running.
1. Create an Expo project and install Triplit
Create your expo project:
npx create-expo-app -t expo-template-blank-typescript
cd my-app
For more information on setting up an Expo project with typescript see the Expo documentation (opens in a new tab).
Next, install Triplit's packages:
npm i @triplit/client @triplit/react-native
npm i @triplit/cli --save-dev
2. Update metro.config.js
Triplit has a few packages that use features unsupported by the Metro bundler like exports (opens in a new tab). Triplit provides a utility for generating a custom Metro config that will resolve these exports.
To handle Triplit's packages you will need to customize the Metro bundler. If you have not already created a metro.config.js
file, please see the Expo docs on properly configuring Metro (opens in a new tab). Once you have created a metro.config.js
file, you can add the following code to properly resolve Triplit packages:
const { getDefaultConfig } = require('expo/metro-config');
const config = getDefaultConfig(__dirname);
const { triplitMetroConfig } = require('@triplit/react-native/metro-config');
module.exports = triplitMetroConfig(config);
If you would like more control over dependency resolution, you can import triplitMetroResolveRequest
and use it inside a custom resolver.
const { getDefaultConfig } = require('expo/metro-config');
const config = getDefaultConfig(__dirname);
const {
triplitMetroResolveRequest,
} = require('@triplit/react-native/metro-config');
config.resolver.resolveRequest = (context, moduleName, platform) => {
const triplitResult = triplitMetroResolveRequest(moduleName);
if (triplitResult) return triplitResult;
// Additional resolver logic
return context.resolveRequest(context, moduleName, platform);
};
module.exports = config;
3. Configure Babel (optional)
If you are building for the web, you'll need to update a babel configuration file. At the root of your Expo project, create a babel.config.js
file with the following content:
module.exports = function (api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
assumptions: {
enumerableModuleMeta: true,
},
};
};
4. Configure polyfills
Triplit was originally built to run in web browsers, so a few APIs are used in some core packages and dependencies that are not in the ECMAScript spec that Hermes implements. So you will need to add some polyfills to your project.
These polyfills should be imported or implemented in your project's entry file so they can be run as early as possible. Typically this is your index.js
file. If you are using Expo Router see this thread (opens in a new tab) on creating and using an index.js
file to add polyfills.
// Import polyfills relevant to Triplit
import '@triplit/react-native/polyfills';
// ... other polyfills
// If using Expo Router:
import 'expo-router/entry';
// The rest of your entry file
Local development
When running a local development server on your machine, it will be running at localhost
. However if you are running your app on a physical device (ie your phone with the Expo Go app or a custom build) you will need to change the localhost
to your machine's IP address. You can find your IP address by running ipconfig getifaddr en0
in your terminal. So a URL http://localhost:<port>
would become http://<your-ip>:<port>
.
Storage providers
Triplit provides storage providers for React Native applications to persist data on the device, including for expo-sqlite
. Read more about the available storage providers in the client storage documentation.
Bare React Native
The team over at Triplit hasn't had the chance to test out a bare React Native project. Although we don't expect the required steps to be much different than with Expo, there may be differences. If you have set up Triplit in a bare RN project, please let us know how it went!