npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-simple-store2

v1.0.0

Published

A minimalistic wrapper around React Native's AsyncStorage.

Downloads

2

Readme

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

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

// 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