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

  1. Installation, NPM/YARN installation
npm install simple-browser-store
yarn add simple-browser-store
Copy the code
  1. 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
  1. 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
  1. GetData returns an object containing key-value pairs (not null)

  2. SetData updates or adds key-value pairs using object. assign