11# Async Storage Core
22
3- Main Async Storage component .
3+ Public-facing API of Async Storage.
44
5- ## Installation
5+ ## Install
66
77``` bash
88$ yarn install @react-native-community/async-storage
99```
1010
11- ## Usage
11+ ## API
1212
13- Use ` AsyncStorageFactory ` to create your Async Storage instance
1413
14+ ### ` AsyncStorageFactory `
15+
16+ A factory module for ` AsyncStorage ` instance with selected storage backend attached.
1517
16- ``` typescript
1718
19+ ``` typescript
1820// storage.js
1921
2022import ASFactory from ' @react-native-community/async-storage'
2123
2224// use any available Storage Backend
2325const storageBackend = new StorageBackend ();
2426
25- const mobileStorage = ASFactory .create (storageBackend );
27+ const mobileStorage = ASFactory .create (storageBackend , options );
2628
2729export default mobileStorage ;
2830```
2931
30- ## Providing a storage model
3132
32- You can provide a ` Model ` type when creating Async Storage, to have a fully typed storage.
33+ ** Factory options**
34+
35+ ` AsyncStorageFactory.create ` accepts an options object, that enables additional features.
36+
37+
38+ - * logger*
39+
40+ ``` typescript
41+ type logger = ((action : LoggerAction ) => void ) | boolean ;
42+ ```
43+
44+ Used to log ` AsyncStorage ` method calls and used arguments.
45+ You can provide your own implementation or use provided default logger (enabled by default in DEV mode).
46+
47+
48+ - * errorHandler*
49+
50+ ``` typescript
51+ type errorHandler = ((error : Error | string ) => void ) | boolean ;
52+ ````
53+
54+ Used to report any errors thrown .
55+ You can provide your own implementation or use provided default error handler (enabled by default in DEV mode ).
56+
57+
58+ ** Providing a storage model **
59+
60+ If you know the structure of the stored data upfront , you can use the full potential of the type system , by providing a type argument to `AsyncStorageFactory.create<T >` method.
3361
3462
3563` ` ` typescript
@@ -43,17 +71,19 @@ type StorageModel = {
4371 preferences: {
4472 darkModeEnabled: boolean;
4573 };
46- subscribedChannels: Array <Channel >;
74+ subscribedChannels: Array<string >;
4775 onboardingCompleted: boolean;
4876};
4977
5078// use any available Storage Backend
5179const storageBackend = new StorageBackend();
5280
81+
5382const storage = ASFactory.create<StorageModel>(storageBackend);
5483
5584` ` `
5685
5786## License
5887
59- MIT.
88+ MIT
89+
0 commit comments