项目作者: jasonmerino

项目描述 :
A minimalistic wrapper around React Native's AsyncStorage.
高级语言: JavaScript
项目地址: git://github.com/jasonmerino/react-native-simple-store.git
创建时间: 2015-08-19T04:12:07Z
项目社区:https://github.com/jasonmerino/react-native-simple-store

开源协议:MIT License

下载


React Native Simple Store

Code Climate
Build Status
npm version
npm

A minimalistic wrapper around React Native’s AsyncStorage.

The react-native-simple-store is a good match for apps that are not using redux. If you have already found that your app needs to use redux and you need to persist data to the device it is recommended that you make use of redux-persist which provides a clean interface for storing data in your reducers to device.

Installation

  1. npm install react-native-simple-store

Since this wrapper uses react-native-async-storage, it needs to be linked to work properly:

  1. react-native link @react-native-community/async-storage

Use In Project

  1. import store from 'react-native-simple-store';

API Reference


Example Usage


Working With Objects


React-native-simple-store allows you to easily store data by assigning it a unique key. We will show you a few examples of just how easy it is to get started.

Saving and Retrieval

Updating

  1. // Update the object stored under the key 'album'. We will add a new property of 'albumName' to this object.
  2. store.update('album', {
  3. albumName: 'Blurry Face'
  4. })
  5. // Get updated object
  6. store.get('album')
  7. .then((res) =>
  8. console.log(res.albumName) // 'Blurry Face'
  9. )
  10. // Our object stored under the key 'album' now looks like this
  11. {
  12. artist: 'Twenty One Pilots',
  13. albumName: 'Blurry Face'
  14. }

Working With Arrays


Arrays are easy to work with using react-native-simple-store’s built-in “push” method. You can use the “push” method to create an array, or add data to the array. Behind the scene’s react-native-simple-store will check if an array exists under the key you specified, if it does, it will add the new specified data to the existing array. If it does not exist, it will create the array for you.

Array Creation

  1. // Save an array to the users device. We will give it the key 'shoppingList' for easy retrieval
  2. store.push("shoppingList", "milk");
  3. // ['milk'] is created and stored on the users device

Retrieval and Updating

  1. // Get the array from the users device
  2. store.get("shoppingList").then(
  3. res => console.log(res) // ['milk']
  4. );
  5. // Add data to 'shoppingList'
  6. store.push("shoppingList", "coffee");
  7. // Retrieve new array
  8. store.get("shoppingList").then(
  9. res => console.log(res) // ['milk', 'coffee']
  10. );

More “Advanced” Example

Instead of storing strings in an array like the above example, let’s store objects. We will create a new array to store on the user’s device named ‘artists’.

  1. const femaleArtist = {
  2. name: "Lady Gaga",
  3. age: 31,
  4. gender: "female"
  5. };
  6. const maleArtist = {
  7. name: "The Weeknd",
  8. age: 27,
  9. gender: "male"
  10. };
  11. // Creates a new array, and inserts this object into it.
  12. store.push("artists", femaleArtist);
  13. // Adds this new object to the end of the array.
  14. store.push("artists", maleArtist);
  15. // Our new array will look like this when we retrieve it
  16. // [{
  17. // name: "Lady Gaga",
  18. // age: 31,
  19. // gender: "female"
  20. // },
  21. // {
  22. // name: "The Weeknd",
  23. // age: 27,
  24. // gender: "male"
  25. // }];

Chaining


You can chain these methods as much as you’d like, as well as catch errors. Here is a lengthy example for you to reference.

  1. store
  2. .save("coffee", {
  3. isAwesome: true
  4. })
  5. .then(() => store.get("coffee"))
  6. .then(coffee => {
  7. console.assert(coffee.isAwesome === true);
  8. })
  9. .then(() =>
  10. store.update("coffee", {
  11. isNotEssential: false
  12. })
  13. )
  14. .then(() => store.get("coffee"))
  15. .then(coffee => {
  16. console.assert(coffee.isNotEssential === false);
  17. console.assert(coffee.isAwesome === true);
  18. return store.delete("coffee");
  19. })
  20. .then(() => store.get("coffee"))
  21. .then(coffee => {
  22. console.assert(coffee === null);
  23. })
  24. .catch(error => {
  25. console.error(error.message);
  26. });
  27. // using the .push method for storing arrays
  28. store
  29. .save("coffeeTraits", ["rich"])
  30. .then(store.push("coffeeTraits", "smooth"))
  31. .then(store.get("coffeeTraits"))
  32. .then(console.log); // ['rich', 'smooth']

Deleting Data


Deleting the data on the user’s device is just as easy. Just insert the key of the data you want to remove as the argument to the “delete” method, and you are done!

  1. store.delete("album"); // Bye bye

License

MIT