diff --git a/src/RCTAsyncStorage.expo.js b/src/RCTAsyncStorage.expo.js deleted file mode 100644 index f290a5bf..00000000 --- a/src/RCTAsyncStorage.expo.js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Expo managed apps don't include the @react-native-async-storage/async-storage - * native modules yet, but the API interface is the same, so we can use the version - * exported from React Native still. - * - * If in future releases (eg: @react-native-async-storage/async-storage >= 2.0.0) this - * will likely not be valid anymore, and the package will need to be included in the Expo SDK - * to continue to work. - */ -const {NativeModules, TurboModuleRegistry} = require('react-native'); - -let RCTAsyncStorage; - -// TurboModuleRegistry falls back to NativeModules -// so we don't have to try go assign NativeModules' -// counterparts if TurboModuleRegistry would resolve -// with undefined. -if (TurboModuleRegistry) { - RCTAsyncStorage = TurboModuleRegistry.get('AsyncSQLiteDBStorage') || TurboModuleRegistry.get('AsyncLocalStorage'); -} else { - RCTAsyncStorage = NativeModules.AsyncSQLiteDBStorage || NativeModules.AsyncLocalStorage; -} - -export default RCTAsyncStorage; diff --git a/src/RCTAsyncStorage.js b/src/RCTAsyncStorage.js index ef0412d9..e2dd335f 100644 --- a/src/RCTAsyncStorage.js +++ b/src/RCTAsyncStorage.js @@ -1,8 +1,20 @@ -const {NativeModules} = require('react-native'); +const {NativeModules, TurboModuleRegistry} = require('react-native'); +const shouldFallbackToLegacyNativeModule = require('./shouldFallbackToLegacyNativeModule'); -const RCTAsyncStorage = +let RCTAsyncStorage = NativeModules.PlatformLocalStorage || // Support for external modules, like react-native-windows NativeModules.RNC_AsyncSQLiteDBStorage || NativeModules.RNCAsyncStorage; +if (!RCTAsyncStorage && shouldFallbackToLegacyNativeModule()) { + // TurboModuleRegistry falls back to NativeModules so we don't have to try go + // assign NativeModules' counterparts if TurboModuleRegistry would resolve + // with undefined. + if (TurboModuleRegistry) { + RCTAsyncStorage = TurboModuleRegistry.get('AsyncSQLiteDBStorage') || TurboModuleRegistry.get('AsyncLocalStorage'); + } else { + RCTAsyncStorage = NativeModules.AsyncSQLiteDBStorage || NativeModules.AsyncLocalStorage; + } +} + export default RCTAsyncStorage; diff --git a/src/shouldFallbackToLegacyNativeModule.js b/src/shouldFallbackToLegacyNativeModule.js new file mode 100644 index 00000000..54257898 --- /dev/null +++ b/src/shouldFallbackToLegacyNativeModule.js @@ -0,0 +1,30 @@ +const {NativeModules} = require('react-native'); + +export default function shouldFallbackToLegacyNativeModule() { + const expoConstants = + NativeModules.NativeUnimoduleProxy?.modulesConstants?.ExponentConstants; + + if (expoConstants) { + /** + * In SDK <= 39, appOwnership is defined in managed apps but executionEnvironment is not. + * In bare React Native apps using expo-constants, appOwnership is never defined, so + * isLegacySdkVersion will be false in that context. + */ + const isLegacySdkVersion = expoConstants.appOwnership && !expoConstants.executionEnvironment; + + /** + * Expo managed apps don't include the @react-native-async-storage/async-storage + * native modules yet, but the API interface is the same, so we can use the version + * exported from React Native still. + * + * If in future releases (eg: @react-native-async-storage/async-storage >= 2.0.0) this + * will likely not be valid anymore, and the package will need to be included in the Expo SDK + * to continue to work. + */ + if (isLegacySdkVersion || ['storeClient', 'standalone'].includes(expoConstants.executionEnvironment)) { + return true; + } + } + + return false; +} diff --git a/website/docs/Installation.md b/website/docs/Installation.md index 207a76cb..473e7cfc 100644 --- a/website/docs/Installation.md +++ b/website/docs/Installation.md @@ -4,8 +4,6 @@ title: Installation sidebar_label: Installation --- - - ### Get library With npm: @@ -18,6 +16,11 @@ With Yarn: yarn add @react-native-async-storage/async-storage ``` +With Expo CLI: +```bash +expo install @react-native-async-storage/async-storage +``` + ### Link #### Android & iOS