Custom storage
By default, this library attempts to use @react-native-async-storage/async-storage to persist the "first entrance" flag. However, you can inject your own storage implementation.
This is useful if you:
- Use MMKV for performance.
- Use Expo Secure Store for sensitive data.
Usage
To use a custom storage, pass an object implementing the DetourStorage interface to the storage property in the config.
The interface is simple:
interface DetourStorage {
getItem(key: string): Promise<string | null> | string | null;
setItem(key: string, value: string): Promise<void> | void;
removeItem?(key: string): Promise<void> | void;
}
Example: React Native MMKV
import { createMMKV } from "react-native-mmkv";
import { DetourProvider } from "@swmansion/react-native-detour";
const storage = createMMKV();
const mmkvAdapter = {
// MMKV returns undefined if missing, but we expect null
getItem: (key: string) => storage.getString(key) ?? null,
setItem: (key: string, value: string) => storage.set(key, value),
};
export default function App() {
return (
<DetourProvider
config={{
API_KEY: "...",
appID: "...",
storage: mmkvAdapter,
}}
>
{/* Your App */}
</DetourProvider>
);
}
Example: Expo Secure Store
import * as SecureStore from "expo-secure-store";
import { DetourProvider } from "@swmansion/react-native-detour";
const secureStoreAdapter = {
getItem: SecureStore.getItemAsync,
setItem: SecureStore.setItemAsync,
};
export default function App() {
return (
<DetourProvider
config={{
API_KEY: "...",
appID: "...",
storage: secureStoreAdapter,
}}
>
{/* Your App */}
</DetourProvider>
);
}