Top Related Projects
A framework for building native applications using React
Realm is a mobile database: an alternative to SQLite & key-value stores
An ultra fast (0.0002s read/write), small & encrypted mobile key-value storage framework for React Native written in C++ using JSI
Quick Overview
React Native Async Storage is an asynchronous, persistent, key-value storage system for React Native applications. It provides a simple and efficient way to store data locally on the device, allowing developers to persist app state and user preferences across app launches.
Pros
- Simple API that closely resembles the Web Storage API
- Supports both iOS and Android platforms
- Asynchronous operations prevent blocking the main thread
- Automatic data serialization and deserialization for JavaScript objects
Cons
- Limited storage capacity (varies by device)
- No built-in encryption for sensitive data
- Potential performance issues with large datasets
- No support for complex querying or indexing
Code Examples
- Storing a value:
import AsyncStorage from '@react-native-async-storage/async-storage';
const storeData = async (value) => {
try {
await AsyncStorage.setItem('my-key', value);
} catch (e) {
// saving error
}
};
- Retrieving a value:
import AsyncStorage from '@react-native-async-storage/async-storage';
const getData = async () => {
try {
const value = await AsyncStorage.getItem('my-key');
if (value !== null) {
// value previously stored
}
} catch (e) {
// error reading value
}
};
- Storing and retrieving an object:
import AsyncStorage from '@react-native-async-storage/async-storage';
const storeObject = async (value) => {
try {
const jsonValue = JSON.stringify(value);
await AsyncStorage.setItem('my-object-key', jsonValue);
} catch (e) {
// saving error
}
};
const getObject = async () => {
try {
const jsonValue = await AsyncStorage.getItem('my-object-key');
return jsonValue != null ? JSON.parse(jsonValue) : null;
} catch (e) {
// error reading value
}
};
Getting Started
- Install the package:
npm install @react-native-async-storage/async-storage
- For React Native 0.60 and above, the library is automatically linked. For earlier versions, you may need to run:
react-native link @react-native-async-storage/async-storage
- Import and use in your React Native app:
import AsyncStorage from '@react-native-async-storage/async-storage';
// Use AsyncStorage methods in your components
const storeData = async (value) => {
try {
await AsyncStorage.setItem('my-key', value);
} catch (e) {
console.error('Error storing data:', e);
}
};
Competitor Comparisons
A framework for building native applications using React
Pros of React Native
- Comprehensive framework for building cross-platform mobile apps
- Large ecosystem with extensive documentation and community support
- Includes built-in UI components and APIs for various device features
Cons of React Native
- Larger project size and complexity compared to focused libraries
- Steeper learning curve for developers new to mobile development
- May include unnecessary features for projects only needing storage functionality
Code Comparison
React Native (basic component):
import React from 'react';
import { View, Text } from 'react-native';
const MyComponent = () => (
<View>
<Text>Hello, React Native!</Text>
</View>
);
Async Storage (basic usage):
import AsyncStorage from '@react-native-async-storage/async-storage';
const storeData = async (value) => {
try {
await AsyncStorage.setItem('myKey', value);
} catch (e) {
// Handle error
}
};
Summary
React Native is a full-featured framework for building mobile applications, while Async Storage is a focused library for data persistence. React Native offers a complete solution but may be overkill for simple storage needs. Async Storage provides a straightforward API for storing data but lacks the comprehensive features of React Native. Choose based on your project's scope and requirements.
Realm is a mobile database: an alternative to SQLite & key-value stores
Pros of realm-js
- More powerful querying capabilities with support for complex queries and indexing
- Better performance for large datasets and frequent read/write operations
- Supports real-time synchronization and offline-first functionality
Cons of realm-js
- Steeper learning curve and more complex setup compared to async-storage
- Larger bundle size and potential impact on app performance for simpler use cases
- Less flexibility in data structure as it requires defining schemas
Code Comparison
async-storage:
import AsyncStorage from '@react-native-async-storage/async-storage';
const storeData = async (value) => {
try {
await AsyncStorage.setItem('key', value);
} catch (e) {
// error handling
}
};
realm-js:
import Realm from 'realm';
const TaskSchema = {
name: 'Task',
properties: {
_id: 'int',
name: 'string',
status: 'string?'
}
};
const realm = await Realm.open({schema: [TaskSchema]});
realm.write(() => {
realm.create('Task', {_id: 1, name: 'New Task'});
});
The code comparison illustrates the simplicity of async-storage for basic key-value storage, while realm-js requires more setup but offers structured data storage with schemas.
An ultra fast (0.0002s read/write), small & encrypted mobile key-value storage framework for React Native written in C++ using JSI
Pros of react-native-mmkv-storage
- Significantly faster performance, especially for large datasets
- Supports encryption out of the box
- Allows storing multiple data types (objects, arrays, numbers, booleans)
Cons of react-native-mmkv-storage
- Larger bundle size due to native dependencies
- Less widespread adoption and community support
- May require additional setup and configuration
Code Comparison
async-storage:
import AsyncStorage from '@react-native-async-storage/async-storage';
await AsyncStorage.setItem('key', 'value');
const value = await AsyncStorage.getItem('key');
react-native-mmkv-storage:
import MMKVStorage from 'react-native-mmkv-storage';
const MMKV = new MMKVStorage.Loader().initialize();
MMKV.setStringAsync('key', 'value');
const value = await MMKV.getStringAsync('key');
Both libraries provide similar APIs for basic storage operations, but react-native-mmkv-storage offers additional methods for different data types and encryption. The initialization process for react-native-mmkv-storage is slightly more verbose, requiring the creation of an instance before use.
While async-storage is the official React Native solution and offers simplicity and broad compatibility, react-native-mmkv-storage provides enhanced performance and features at the cost of a more complex setup and larger bundle size.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
React Native Async Storage
Async Storage is an asynchronous, unencrypted, persistent key-value storage solution for your React Native application. It provides a simple API compatible with the Web Storage API, with additional extensions for batch operations and multi-database support.
Supported platforms
- Android (SQLite)
- iOS (SQLite)
- macOS (SQLite)
- visionOS (legacy fallback, single database only)
- Web (IndexedDB backend)
- Windows (legacy fallback, single database only)
Compatibility
Compatibility table for React Native:
| React Native | Minimum Version |
|---|---|
| ios/android | 0.76 |
| macOS | 0.78 |
| visionOS | 0.79 |
| windows | 0.79 |
Other components:
| Component | Version |
|---|---|
| kotlin | 2.1.0 |
| android min sdk | 24 |
| ios min target | 13 |
| macOS min target | 12 |
Installation
# Using npm
npm install @react-native-async-storage/async-storage
# Using yarn
yarn add @react-native-async-storage/async-storage
Android
Inside your android/build.gradle(.kts) file, add link to local maven repo:
allprojects {
repositories {
// ... others like google(), mavenCentral()
maven {
url = uri(project(":react-native-async-storage_async-storage").file("local_repo"))
// or uri("path/to/node_modules/@react-native-async-storage/async-storage/android/local_repo")
}
}
}
iOS/macOS
Install cocoapods dependencies:
# inside macos/ios directory
pod install
Usage
import { createAsyncStorage } from "@react-native-async-storage/async-storage";
// create a storage instance
const storage = createAsyncStorage("appDB");
async function demo() {
await storage.setItem("userToken", "abc123");
const token = await storage.getItem("userToken");
console.log("Stored token:", token); // abc123
await storage.removeItem("userToken");
}
License
MIT
Top Related Projects
A framework for building native applications using React
Realm is a mobile database: an alternative to SQLite & key-value stores
An ultra fast (0.0002s read/write), small & encrypted mobile key-value storage framework for React Native written in C++ using JSI
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot