Simple -browser-store Instruction
Characteristics: based on the object keys to format the front-end data, unified handling cookies/localStorage/sessionStorage, data update similar react. SetState () is always part of the update
- Installation, NPM/YARN installation
npm install simple-browser-store
yarn add simple-browser-store
Copy the code
- use
import { getData, setData, removeData } from 'simple-browser-store';
/ / cookie store
const appKey = '__app__';
const cookieData = getData('cookie', appKey);
setData('cookie', appKey, { newProp: 'new prop value' });
/ / localStorage storage
const localStorageData = getData('localStorage', appKey);
setData('localStorage', appKey, { newProp: 'new prop value' });
Copy the code
- Typescript type definitions
export declare type StorageType = 'cookie' | 'localStorage' | 'sessionStorage';
export declare const getData: (type: StorageType, key: string) = > Record<string, unknown>;
export declare const setData: (type: StorageType, key: string, value: Record
, cookieOptions? : CookieAttributes
,>) = > void;
export declare const removeData: (type: StorageType, key: string) = > void;
Copy the code
-
GetData returns an object containing key-value pairs (not null)
-
SetData updates or adds key-value pairs using object. assign