diff --git a/packages/sdk/react-native/package.json b/packages/sdk/react-native/package.json index 78f2fa9bc2..c04de716a8 100644 --- a/packages/sdk/react-native/package.json +++ b/packages/sdk/react-native/package.json @@ -40,7 +40,8 @@ "ios": "yarn && yarn ./example && yarn build && (cd example/ && yarn ios-go)" }, "peerDependencies": { - "react": "*" + "react": "*", + "react-native": "*" }, "dependencies": { "@launchdarkly/js-client-sdk-common": "0.0.1", @@ -64,6 +65,7 @@ "launchdarkly-js-test-helpers": "^2.2.0", "prettier": "^3.0.0", "react": "^18.2.0", + "react-native": "^0.73.1", "rimraf": "^5.0.5", "ts-jest": "^29.1.0", "typedoc": "0.25.0", diff --git a/packages/sdk/react-native/src/platform/ConditionalAsyncStorage.ts b/packages/sdk/react-native/src/platform/ConditionalAsyncStorage.ts new file mode 100644 index 0000000000..8f4f111a61 --- /dev/null +++ b/packages/sdk/react-native/src/platform/ConditionalAsyncStorage.ts @@ -0,0 +1,30 @@ +/* eslint-disable import/no-mutable-exports,global-require */ + +/** + * For react-native version >= 0.71, the LaunchDarkly React-Native SDK uses + * @react-native-async-storage/async-storage for bootstrapping. This is a native + * dependency. + * + * If you are using expo, then adding the LaunchDarkly React Native + * SDK from npm and re-running pod install should suffice. + * + * If you are not using expo, you will need to explicitly add + * @react-native-async-storage/async-storage as a dependency to your project + * and re-run pod install for auto-linking to work. This is because auto-link + * does not work with transitive dependencies: + * https://github.com/react-native-community/cli/issues/1347 + * + * For react-native version < 0.71, the built-in react-native AsyncStorage + * module is used. + */ +let ConditionalAsyncStorage: any; + +try { + // react-native version < 0.71 + ConditionalAsyncStorage = require('react-native').AsyncStorage; +} catch (e) { + // react-native version >= 0.71 + ConditionalAsyncStorage = require('@react-native-async-storage/async-storage').default; +} + +export default ConditionalAsyncStorage; diff --git a/packages/sdk/react-native/src/platform.ts b/packages/sdk/react-native/src/platform/index.ts similarity index 74% rename from packages/sdk/react-native/src/platform.ts rename to packages/sdk/react-native/src/platform/index.ts index ee85050cf6..ba2db6299b 100644 --- a/packages/sdk/react-native/src/platform.ts +++ b/packages/sdk/react-native/src/platform/index.ts @@ -1,20 +1,4 @@ /* eslint-disable max-classes-per-file */ - -/** - * The LaunchDarkly SDK uses async-storage for bootstrapping and this is a native - * dependency. - * - * If you are using expo, then adding the LaunchDarkly React Native - * SDK from npm and re-running pod install should suffice. - * - * If you are not using expo, you will need to explicitly add - * @react-native-async-storage/async-storage as a dependency to your project - * and re-run pod install for auto-linking to work. This is because auto-link - * does not work with transitive dependencies: - * https://github.com/react-native-community/cli/issues/1347 - */ -import AsyncStorage from '@react-native-async-storage/async-storage'; - import type { Crypto, Encoding, @@ -34,9 +18,10 @@ import type { Storage, } from '@launchdarkly/js-client-sdk-common'; -import { name, version } from '../package.json'; -import { btoa, uuidv4 } from './polyfills'; -import RNEventSource from './react-native-sse'; +import { name, version } from '../../package.json'; +import { btoa, uuidv4 } from '../polyfills'; +import RNEventSource from '../react-native-sse'; +import AsyncStorage from './ConditionalAsyncStorage'; class PlatformRequests implements Requests { createEventSource(url: string, eventSourceInitDict: EventSourceInitDict): EventSource {