|
| 1 | +--- |
| 2 | +title: Usage |
| 3 | +--- |
| 4 | + |
| 5 | +# Using Async Storage |
| 6 | + |
| 7 | +The [`AsyncStorage`](https://github.com/react-native-async-storage/async-storage/blob/main/packages/async-storage/src/AsyncStorage.ts) interface provides an asynchronous, promise-based API for persistent key-value storage. |
| 8 | +Each method is modeled after the Web Storage API, with extensions for batch operations. |
| 9 | + |
| 10 | +Similar to Web, AsyncStorage deals with data that should be already serialized (strings). If you need to store objects, arrays, or other non-string values, you must serialize them first (for example, using `JSON.stringify`) and deserialize them when retrieving (for example, using `JSON.parse`). |
| 11 | + |
| 12 | +## Creating a storage |
| 13 | + |
| 14 | +To create a new storage, call `createAsyncStorage` with your database name: |
| 15 | + |
| 16 | +```typescript |
| 17 | +import { createAsyncStorage } from "@react-native-async-storage/async-storage"; |
| 18 | + |
| 19 | +// any name is fine, you can even add an extension! |
| 20 | +const userStorage = createAsyncStorage("john.db"); |
| 21 | +``` |
| 22 | + |
| 23 | +This returns an instance of `AsyncStorage` and each instance is uniquely identified by the name you provide. |
| 24 | +The data in one storage instance is scoped to its name: using different names ensures that data does not leak between storages. |
| 25 | + |
| 26 | +!!! note "Web platform" |
| 27 | + |
| 28 | + On Web, AsyncStorage is backed by [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API), which also support scoped storages. |
| 29 | + |
| 30 | +!!! warning "Windows platform" |
| 31 | + |
| 32 | + As of AsyncStorage v3.0, Windows platform does not support scoped storages. It fallsback to previous implementation - single storage per application. |
| 33 | + |
| 34 | +## Using a storage |
| 35 | + |
| 36 | +Once you have created a storage instance, you can start managing data. |
| 37 | + |
| 38 | +### Single item operations |
| 39 | + |
| 40 | +You can store, retrieve, and remove individual keys using `setItem`, `getItem`, and `removeItem`. |
| 41 | + |
| 42 | +Note that: |
| 43 | + |
| 44 | +- Calling `setItem` with an existing key will overwrite the current value |
| 45 | +- Calling `removeItem` on a key that does not exist has no effect and does not throw an error |
| 46 | + |
| 47 | +```typescript |
| 48 | +await userStorage.setItem("username", "doe_john"); |
| 49 | +// previously stored value is overriden |
| 50 | +await userStorage.setItem("username", "john_doe"); |
| 51 | + |
| 52 | +// read current value |
| 53 | +let username = await userStorage.getItem("username"); |
| 54 | +console.log(username); // "john_doe" |
| 55 | + |
| 56 | +await userStorage.removeItem("username"); |
| 57 | +// does nothing, item is already removed |
| 58 | +await userStorage.removeItem("username"); |
| 59 | + |
| 60 | +username = await userStorage.getItem("username"); |
| 61 | +console.log(username); // null |
| 62 | +``` |
| 63 | + |
| 64 | +### Batch operations |
| 65 | + |
| 66 | +Use convenient batch methods to handle multiple keys at once. Behind the scene, transaction performed to store all of them, or none in case of an error. |
| 67 | + |
| 68 | +```typescript |
| 69 | +// store values |
| 70 | +await userStorage.setMany({ |
| 71 | + |
| 72 | + age: "30", |
| 73 | +}); |
| 74 | + |
| 75 | +// read multiple items |
| 76 | +const data = await userStorage.getMany(["email", "age", "username"]); |
| 77 | +console.log(data); |
| 78 | +// { |
| 79 | + |
| 80 | +// age: "30", |
| 81 | +// username: null, // key doesn't exist |
| 82 | +// } |
| 83 | + |
| 84 | +// remove multiple items |
| 85 | +// non-existing keys are ignored |
| 86 | +await userStorage.removeMany(["email", "age", "not-here"]); |
| 87 | +``` |
| 88 | + |
| 89 | +### Reading stored keys |
| 90 | + |
| 91 | +To retrieve all keys currently stored in a storage instance, use `getAllKeys`: |
| 92 | + |
| 93 | +```typescript |
| 94 | +await userStorage.setMany({ |
| 95 | + |
| 96 | + age: "30", |
| 97 | +}); |
| 98 | + |
| 99 | +const keys = await userStorage.getAllKeys(); |
| 100 | +console.log(keys); // ["email", "age"] |
| 101 | +``` |
| 102 | + |
| 103 | +### Clearing storage |
| 104 | + |
| 105 | +To remove all data from a storage instance, use `clear`: |
| 106 | + |
| 107 | +```typescript |
| 108 | +await userStorage.setMany({ |
| 109 | + |
| 110 | + age: "30", |
| 111 | +}); |
| 112 | + |
| 113 | +await userStorage.clear(); |
| 114 | +const keys = await userStorage.getAllKeys(); |
| 115 | +console.log(keys); // [] |
| 116 | +``` |
0 commit comments