GithubHelp home page GithubHelp logo

najihmld / expo-fs-storage Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 275 KB

An persistent, key-value storage system for React Native Expo, which does not have size limitations of @react-native-async-storage/async-storage.

TypeScript 100.00%

expo-fs-storage's Introduction

@najihmld/expo-fs-storage

An persistent, key-value storage system for React Native Expo, which does not have size limitations of @react-native-async-storage/async-storage

Installation

With npm:

npm install @najihmld/expo-fs-storage

With Yarn:

yarn add @najihmld/expo-fs-storage

With Expo CLI:

npx expo install @najihmld/expo-fs-storage

Usage

ExpoFsStorage can only store string data. In order to store object data, you need to serialize it first. For data that can be serialized to JSON, you can use JSON.stringify() when saving the data and JSON.parse() when loading the data.

Importing

import ExpoFsStorage from "@najihmld/expo-fs-storage";

Storing data

setItem() is used both to add new data item (when no data for given key exists), and to modify existing item (when previous data for given key exists).

Storing string value

const storeData = async (value) => {
  try {
    await ExpoFsStorage.setItem("my-key", value);
  } catch (e) {
    // saving error
  }
};

Storing object value

const storeData = async (value) => {
  try {
    const jsonValue = JSON.stringify(value);
    await ExpoFsStorage.setItem("my-key", jsonValue);
  } catch (e) {
    // saving error
  }
};

Reading data

getItem returns a promise that either resolves to stored value when data is found for given key, or returns null otherwise.

Reading string value

const getData = async () => {
  try {
    const value = await ExpoFsStorage.getItem("my-key");
    if (value !== null) {
      // value previously stored
    }
  } catch (e) {
    // error reading value
  }
};

Reading object value

const getData = async () => {
  try {
    const jsonValue = await ExpoFsStorage.getItem("my-key");
    return jsonValue != null ? JSON.parse(jsonValue) : null;
  } catch (e) {
    // error reading value
  }
};

Remove data

Removes item for a key, invokes (optional) callback once completed.

const removeData = async () => {
  try {
    await ExpoFsStorage.removeItem("my-key");
  } catch (e) {
    // remove error
  }

  console.log("Done.");
};

Using zustand for persist your store's data

import ExpoFsStorage from "@najihmld/expo-fs-storage";
import { create } from "zustand";
import { persist, createJSONStorage } from "zustand/middleware";

const useFishStore = create(
  persist(
    (set, get) => ({
      fishes: 0,
      addAFish: () => set({ fishes: get().fishes + 1 }),
    }),
    {
      name: "expo-storage", // name of the item in the storage (must be unique)
      storage: createJSONStorage(() => ExpoFsStorage),
    }
  )
);

export default useFishStore;

expo-fs-storage's People

Contributors

najihmld avatar

Watchers

 avatar

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.