An asynchronous, persistent, key-value storage system for React Native.
# Install
$ yarn add @react-native-community/async-storage
# Link
$ react-native link @react-native-community/async-storage
See docs for manual linking guide.
import AsyncStorage from '@react-native-community/async-storage';
storeData = async () => {
try {
await AsyncStorage.setItem('@storage_Key', 'stored value')
} catch (e) {
// saving error
}
}
getData = async () => {
try {
const value = await AsyncStorage.getItem('@storage_Key')
if(value !== null) {
// value previously stored
}
} catch(e) {
// error reading value
}
}
React hooks (introduced in 16.8) allow you to use state and async requests without writing a class. For more info on hooks and how to use them, see hooks documentation or the hooks example in this repo.
useAsyncStorage
has no hard-coded dependencies in react hooks, it is just a convenience wrapper around AsyncStorage
.
import { useAsyncStorage } from '@react-native-community/async-storage';
const {
getItem,
setItem,
mergeItem,
removeItem
} = useAsyncStorage('@storage_key');
See docs for api and more examples.
See the CONTRIBUTING file for how to help out.
MIT