Skip to main content

Localstorage

Library to use localstorage easily

Online documentation

Mozilla documentation

Installation

npm install @awesome-cordova-library/localstorage

Vanilla

Declartion

class LocalStorage {
static setItem<T = any>(key: string, value: T): void;
static getItem<T = any>(key: string): T | null;
static removeItem(key: string): void;
static clear(): void;
}

Usages

import LocalStorage from "@awesome-cordova-library/localstorage";

LocalStorage.setItem<string[]>("item", value);
LocalStorage.getItem<string[]>("item");
LocalStorage.removeItem("item");
LocalStorage.clear();

React

Declaration

const useLocalStorage: () => {
setItem: <T = any>(key: string, value: T) => void;
getItem: <T_1 = any>(key: string) => T_1 | null;
removeItem: (key: string) => void;
clear: () => void;
};

Usages

import { useEffect } from "react";
import useLocalStorage from "@awesome-cordova-library/localstorage/lib/react";

function App() {
const { setItem, getItem, removeItem, clear } = useLocalStorage();

useEffect(() => {
setItem<string[]>("item", value);
getItem<string[]>("item");
removeItem("item");
clear();
}, []);

return <div />;
}