React Native Async Storage

repository·main·Indexed 26 days ago

https://github.com/react-native-async-storage/async-storage

An asynchronous, unencrypted, persistent key-value storage solution for React Native applications. It is compatible with the Web Storage API and supports multi-database operations via SQLite on Android, iOS, and macOS, and IndexedDB on Web. The library provides scoped storage via createAsyncStorage, batch operations, and specialized error handling through AsyncStorageError.

Tokens
9.6K
Snippets
33
Records
59
Agent score
89%

What's inside @react-native-async-storage/async-storage

  1. Migrate to AsyncStorage v3

    main
    AsyncStorage v3 introduces breaking changes to move from a singleton pattern to an instance-based pattern. Key changes include the removal of callback arguments (all methods are now Promise-based), the removal of mergeItem functionality, and the removal of the useAsyncStorage hook. Errors now use a standardized AsyncStorageError type with a type property.
  2. Configure Jest to transform AsyncStorage ESM source

    main

    Because @react-native-async-storage/async-storage is shipped as ESM source, you must configure Jest to transform it. Add the package to your transformIgnorePatterns in your Jest configuration file to prevent Jest from ignoring it during the transformation process.

    transformIgnorePatterns: [
        'node_modules/(?!@react-native-async-storage/)',
    ],
  3. Integrate AsyncStorage in iOS / macOS Brownfield apps

    main

    For iOS and macOS brownfield applications, you can access the shared storage layer from Swift or Objective-C using the StorageRegistry singleton. This allows native code to read from and write to the same storage used by the React Native layer.

    Access the storage by calling StorageRegistry.shared.getStorage(dbName: "name"). Use setValues and getValues with Entry objects to manage data. Note that these operations are typically asynchronous and should be handled within a Task or similar concurrency construct.

    import AsyncStorage
    import SharedAsyncStorage
    
    // access shared storage via StorageRegistry
    let storage: SharedStorage = StorageRegistry.shared.getStorage(dbName: "my-users")
    
    Task {
        storage.setValues([Entry(key: "email", value: "john@example.com")])
        let values = storage.getValues(keys: ["email"])
        print("Stored email: \(values.first?.value ?? "none")")
    }
  4. Configure the Expo config plugin in app.json

    main

    To apply the plugin, add @react-native-async-storage/expo-with-async-storage to the plugins array in your app.json file. After updating the configuration, you must run expo prebuild or eas build to apply the changes to your native project files.

    Requirement: This plugin requires expo >= 53.

    {
      "expo": {
        "plugins": [
          "@react-native-async-storage/expo-with-async-storage"
        ]
      }
    }
  5. Build the Apple (iOS/macOS) shared-storage SDK

    main

    The shared-storage module for Apple platforms is distributed as an xcframework containing both Debug and Release binaries. To build the framework, run the following command. The resulting artifact will be moved to packages/async-storage/apple/Frameworks for distribution.

    yarn build:apple
  6. Install the Expo config plugin for Async Storage

    main

    If you are using an Expo managed or bare project and need to automatically configure @react-native-async-storage/async-storage, use the @react-native-async-storage/expo-with-async-storage config plugin.

    Note: This plugin is no longer required if you are using @react-native-async-storage/async-storage version 3.1.0+, as the Android build artifacts are now provided via Maven Central automatically.

    yarn add @react-native-async-storage/expo-with-async-storage
  7. Store objects, arrays, or non-string values

    main

    AsyncStorage only stores strings. To store complex data types like objects or arrays, you must serialize them using JSON.stringify before writing and JSON.parse when reading them back.

    import { createAsyncStorage } from "@react-native-async-storage/async-storage";
    
    const storage = createAsyncStorage("my-app");
    
    // Storing an object
    const user = { name: "John", age: 30 };
    await storage.setItem("user", JSON.stringify(user));
    
    // Reading it back
    const raw = await storage.getItem("user");
    const user = raw ? JSON.parse(raw) : null;