项目作者: chenzhe142

项目描述 :
a simple cascading-sync AsyncStorage wrapper for React Native
高级语言: JavaScript
项目地址: git://github.com/chenzhe142/react-native-storage-unit.git
创建时间: 2016-09-27T18:00:42Z
项目社区:https://github.com/chenzhe142/react-native-storage-unit

开源协议:MIT License

下载


react-native-storage-unit

A simple AsyncStorage wrapper for React-native.

It can cascading-sync data with each component.

Version

0.0.2

License

MIT

About this repo

This AsyncStorage wrapper was initially created for a React-native offline notebook app.

I created my own implementation, because I want a simple interface to handle get/save/update/delete methods.

In addition, I want to automatically sync data change to each component, and trigger each component’s re-rendering method.

This is a Redux-like implementation for AsyncStorage

Installation

run npm install react-native-storage-unit --save

Usage

The concept of this wrapper is cascading both cached AsyncStorage data and methods
from the app top level to each component.

In order to do this, you should initialize StorageUnit at the entry point of your app:

  1. export default class SampleApp extends React.Component {
  2. constructor() {
  3. super();
  4. //this.state.storage is the cache object
  5. //it has all data from AsyncStorage, and automatically synced with AsyncStorage.
  6. this.state = {
  7. storage: {}
  8. };
  9. //this storageUnit is a wrapper of AsyncStorage, providing get/save/update/delete methods
  10. this.storageUnit = new StorageUnit(["storageKey_1", "storageKey_2"], this._updateStorage.bind(this));
  11. this.storageUnit.fetchData.then((storage) => {
  12. this.setState({ storage });
  13. });
  14. }
  15. _updateStorage(storage) {
  16. this.setState({ storage });
  17. }
  18. }

After initializing this.state.storage and this.storageUnit, you can pass them as props (such as storageUnit={this.storageUnit} to different components.

We can cascade any AsyncStorage data change to any of the components, which will do data syncing.

Components can also easily call methods through this.props.storageUnit and get AsyncStorage data from this.props.storage

Data structure

  1. {
  2. "your_unique_storage_key_1": {
  3. storageKey: "your_unique_storage_key_1",
  4. content: []
  5. },
  6. "your_unique_storage_key_2": {
  7. storageKey: "your_unique_storage_key_1",
  8. content: [singleObj, singleObj]
  9. }
  10. }

Methods

fetchData(): Promise

fetch all data from AsyncStorage when initalizing

saveItem(storage_key, item)

save an object to AsyncStorage. this method will not override existing data.

item:

  1. should be stringified JSON object (result of JSON.stringify(something)).
  2. requires pre-processing. the final object’s structure is { storageKey: "your_unique_storage_key_1, content: [] }

getItem(storage_key)

get an object from AsyncStorage.

Alternatively, you can retrieve data by accessing this.storage.

updateItem(storage_key, singleObj)

update one existing object.

  1. {
  2. "your_unique_storage_key_2": {
  3. storageKey: "your_unique_storage_key_1",
  4. content: [singleObj1, singleObj2]
  5. }
  6. }

you can use updateItem when you want to update singleObj2 to singleObj3

deleteItem(storage_key, singleObj)

delete one existing object.

  1. {
  2. "your_unique_storage_key_2": {
  3. storageKey: "your_unique_storage_key_1",
  4. content: [singleObj1, singleObj2]
  5. }
  6. }

after calling deleteItem("your_unique_storage_key_2", singleObj2), singleObj2 will be removed.

  1. {
  2. "your_unique_storage_key_2": {
  3. storageKey: "your_unique_storage_key_1",
  4. content: [singleObj1]
  5. }
  6. }

Todo

  1. refactor code
  2. use more flexible data structure
  3. handle edge cases

Contributor

Please feel free to create issues & PRs to this repo. Huge thanks!