Giter Site home page Giter Site logo

jasonmerino / react-native-simple-store Goto Github PK

View Code? Open in Web Editor NEW
878.0 12.0 82.0 2.19 MB

A minimalistic wrapper around React Native's AsyncStorage.

Home Page: https://www.npmjs.com/package/react-native-simple-store

License: MIT License

JavaScript 57.67% Java 7.17% Objective-C 25.61% Starlark 9.55%
react-native asyncstorage ios android

react-native-simple-store's Introduction

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

npm install react-native-simple-store

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

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

Use In Project

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

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

// Update the object stored under the key 'album'. We will add a new property of 'albumName' to this object.
store.update('album', {
  albumName: 'Blurry Face'
})

// Get updated object
store.get('album')
.then((res) =>
  console.log(res.albumName) // 'Blurry Face'
)

// Our object stored under the key 'album' now looks like this
{
artist: 'Twenty One Pilots',
  albumName: 'Blurry Face'
}

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

// Save an array to the users device. We will give it the key 'shoppingList' for easy retrieval
store.push("shoppingList", "milk");

// ['milk'] is created and stored on the users device

Retrieval and Updating

// Get the array from the users device
store.get("shoppingList").then(
  res => console.log(res) // ['milk']
);

// Add data to 'shoppingList'
store.push("shoppingList", "coffee");

// Retrieve new array
store.get("shoppingList").then(
  res => console.log(res) // ['milk', 'coffee']
);

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'.

const femaleArtist = {
  name: "Lady Gaga",
  age: 31,
  gender: "female"
};

const maleArtist = {
  name: "The Weeknd",
  age: 27,
  gender: "male"
};

// Creates a new array, and inserts this object into it.
store.push("artists", femaleArtist);
// Adds this new object to the end of the array.
store.push("artists", maleArtist);

// Our new array will look like this when we retrieve it
// [{
//   name: "Lady Gaga",
//   age: 31,
//   gender: "female"
// },
// {
//   name: "The Weeknd",
//   age: 27,
//   gender: "male"
// }];

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.

store
  .save("coffee", {
    isAwesome: true
  })
  .then(() => store.get("coffee"))
  .then(coffee => {
    console.assert(coffee.isAwesome === true);
  })
  .then(() =>
    store.update("coffee", {
      isNotEssential: false
    })
  )
  .then(() => store.get("coffee"))
  .then(coffee => {
    console.assert(coffee.isNotEssential === false);
    console.assert(coffee.isAwesome === true);
    return store.delete("coffee");
  })
  .then(() => store.get("coffee"))
  .then(coffee => {
    console.assert(coffee === null);
  })
  .catch(error => {
    console.error(error.message);
  });

// using the .push method for storing arrays
store
  .save("coffeeTraits", ["rich"])
  .then(store.push("coffeeTraits", "smooth"))
  .then(store.get("coffeeTraits"))
  .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!

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

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.