https://github.com/pradeep1991singh/react-native-asyncstorage
React native AsyncStorage example
https://github.com/pradeep1991singh/react-native-asyncstorage
asyncstorage react-native
Last synced: 3 months ago
JSON representation
React native AsyncStorage example
- Host: GitHub
- URL: https://github.com/pradeep1991singh/react-native-asyncstorage
- Owner: pradeep1991singh
- Created: 2017-03-28T17:25:08.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-03-28T17:32:22.000Z (over 9 years ago)
- Last Synced: 2025-03-20T14:56:09.936Z (over 1 year ago)
- Topics: asyncstorage, react-native
- Language: JavaScript
- Size: 124 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# react-native-AsyncStorage
React native AsyncStorage example
## Installation
Clone repo
```sh
$ git clone https://github.com/pradeep1991singh/react-native-AsyncStorage.git
```
## Run
```sh
$ cd react-native-AsyncStorage
# run ios app
$ react-native run-ios
# run android app
$ react-native run-android
```
## Saving/Retrieving Data
As AsyncStorage name suggests its an asynchronous storage which returns `Promise` object.
### Persisting data
For persisting data in react app there is `AsyncStorage.setItem` method as we have localStorage.setItem in localStorage.
`setItem` takes two parameters as key-value pair. e.g. -
```js
try {
await AsyncStorage.setItem('@MySuperStore:key', 'hi there');
} catch (error) {
// Error saving data
console.log(error);
}
```
### Retrieving data
For getting saved data back there is `AsyncStorage.getItem` method which takes one parameter(key-name). e.g. -
```js
try {
const value = await AsyncStorage.getItem('@MySuperStore:key');
console.log(value); // hi there
} catch (error) {
console.log(error);
}
```
### Remove saved key
For removing Item for a key there is `AsyncStorage.removeItem` method which takes one parameter(key-name). e.g. -
```js
try {
await AsyncStorage.removeItem('@MySuperStore:key');
} catch (error) {
console.log(error);
}
```