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
An asynchronous, unencrypted, persistent, key-value storage system for React Native.
Supported platforms
Getting Started
Head over to the documentation to learn more.
Running E2E locally
Android
- Create and start Android Emulator with Play services, API level 29
- Build app and run tests
yarn bundle:android yarn build:e2e:android yarn test:e2e:android
iOS
- Create and start iPhone 14 simulator with iOS version 16.4
- Build app and run tests
yarn bundle:ios yarn build:e2e:ios yarn test:e2e:ios
Contribution
Pull requests are welcome. Please open an issue first to discuss what you would like to change.
See the CONTRIBUTING file for more information.
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