Skip to content

Commit 724bc68

Browse files
docs: API clarification (#354)
1 parent e315614 commit 724bc68

File tree

2 files changed

+98
-23
lines changed

2 files changed

+98
-23
lines changed

README.md

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,19 @@ $ yarn add @react-native-community/async-storage
2020

2121
- **React Native 0.60+**
2222

23-
2423
[CLI autolink feature](https://github.com/react-native-community/cli/blob/master/docs/autolinking.md) links the module while building the app.
2524

26-
27-
- **React Native <= 0.59**
28-
25+
Run `pod install` in iOS project in order to add RNAsyncStorage cocoapod:
2926

3027
```bash
31-
$ react-native link @react-native-community/async-storage
28+
$ cd ios && pod install
3229
```
3330

31+
- **React Native <= 0.59**
3432

35-
*Note* For `iOS` using `cocoapods`, run:
3633

3734
```bash
38-
$ cd ios/ && pod install
35+
$ react-native link @react-native-community/async-storage
3936
```
4037

4138
See docs for [manual linking guide](docs/Linking.md)
@@ -54,26 +51,48 @@ $ react-native unlink @react-native-community/async-storage
5451

5552
## Usage
5653

57-
### Import
54+
AsyncStorage can only store `string` data, so in order to store object data you need to serialize it first.
55+
For data that can be serialized to JSON you can use `JSON.stringify()` when saving the data and `JSON.parse()` when loading the data.
56+
57+
58+
### Importing
5859

5960
```js
6061
import AsyncStorage from '@react-native-community/async-storage';
6162
```
6263

63-
### Store data
64-
```jsx
64+
### Storing data
65+
66+
`setItem()` is used both to add new data item (when no data for given key exists), and to modify exiting item (when previous data for given key exists).
6567

66-
storeData = async () => {
68+
#### Storing string value
69+
```jsx
70+
const storeData = async (value) => {
6771
try {
68-
await AsyncStorage.setItem('@storage_Key', 'stored value')
72+
await AsyncStorage.setItem('@storage_Key', value)
6973
} catch (e) {
7074
// saving error
7175
}
7276
}
77+
```
7378

79+
#### Storing object value
80+
```jsx
81+
const storeData = async (value) => {
82+
try {
83+
const jsonValue = JSON.stringify(value)
84+
await AsyncStorage.setItem('@storage_Key', jsonValue)
85+
} catch (e) {
86+
// saving error
87+
}
88+
}
7489
```
7590

76-
### Read data
91+
### Reading data
92+
93+
`getItem` returns a promise that either resolves to stored value when data is found for given key, or returns `null` otherwise.
94+
95+
#### Reading string value
7796
```jsx
7897

7998
getData = async () => {
@@ -88,8 +107,23 @@ getData = async () => {
88107
}
89108

90109
```
91-
### Advanced
92-
See docs for [api and more examples](docs/API.md) or [advanced usages](docs/advanced).
110+
#### Reading object value
111+
112+
```jsx
113+
114+
getData = async () => {
115+
try {
116+
const jsonValue = await AsyncStorage.getItem('@storage_Key')
117+
return jsonValue != null ? JSON.parse(jsonValue) : null;
118+
} catch(e) {
119+
// error reading value
120+
}
121+
}
122+
123+
```
124+
125+
## Advanced usage
126+
See docs for [API and more examples](docs/API.md) or [advanced usages](docs/advanced).
93127

94128
## Writing tests
95129

docs/API.md

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222

2323
## `getItem`
2424

25-
Fetches a data for a given `key`, invokes (optional) callback once completed.
25+
Gets a string value for given `key`. This function can either return a string value for existing `key` or return `null` otherwise.
26+
27+
In order to store object value, you need to deserialize it, e.g. using `JSON.parse()`.
28+
29+
*Note (legacy)*: you can use optional callback as an alternative for returned promise.
2630

2731
**Signature**:
2832

@@ -32,15 +36,32 @@ static getItem(key: string, [callback]: ?(error: ?Error, result: ?string) => voi
3236
3337
**Returns**:
3438
35-
`Promise` with data, if exists, `null` otherwise.
39+
`Promise` resolving with a string value, if entry exists for given `key`, or `null` otherwise.
40+
41+
`Promise` can be also rejects in case of underlying storage error.
3642
3743
**Example**:
3844
3945
```js
4046

41-
getMyValue = async () => {
47+
getMyStringValue = async () => {
48+
try {
49+
return await AsyncStorage.getItem('@key')
50+
} catch(e) {
51+
// read error
52+
}
53+
54+
console.log('Done.')
55+
56+
}
57+
```
58+
59+
```js
60+
61+
getMyObject = async () => {
4262
try {
43-
const value = await AsyncStorage.getItem('@MyApp_key')
63+
const jsonValue = await AsyncStorage.getItem('@key')
64+
return jsonValue != null ? JSON.parse(jsonValue) : null
4465
} catch(e) {
4566
// read error
4667
}
@@ -58,7 +79,11 @@ getMyValue = async () => {
5879
5980
## `setItem`
6081
61-
Stores a `value` for the `key`, invokes (optional) `callback` once completed.
82+
Sets a string `value` for given `key`. This operation can either modify an existing entry, if it did exist for given `key`, or add new one otherwise.
83+
84+
In order to store object value, you need to serialize it, e.g. using `JSON.stringify()`.
85+
86+
*Note (legacy)*: you can use optional callback as an alternative for returned promise.
6287
6388
**Signature**:
6489
@@ -68,15 +93,31 @@ static setItem(key: string, value: string, [callback]: ?(error: ?Error) => void)
6893
6994
**Returns**:
7095
71-
`Promise` object.
96+
`Promise` resolving when the set operation is completed.
97+
98+
`Promise` can be also rejects in case of underlying storage error.
7299
73100
**Example**:
74101
75102
```js
76103

77-
setValue = async () => {
104+
setStringValue = async (value) => {
105+
try {
106+
await AsyncStorage.setItem('key', value)
107+
} catch(e) {
108+
// save error
109+
}
110+
111+
console.log('Done.')
112+
}
113+
```
114+
115+
```js
116+
117+
setObjectValue = async (value) => {
78118
try {
79-
await AsyncStorage.setItem('@MyApp_key', 'my secret value')
119+
const jsonValue = JSON.stringify(value)
120+
await AsyncStorage.setItem('key', jsonValue)
80121
} catch(e) {
81122
// save error
82123
}

0 commit comments

Comments
 (0)