simple-browser-store 使用说明
特点: 基于object键值对格式存储前端数据,统一处理cookie/localStorage/sessionStorage,数据更新类似react.setState()总是部分更新
npm install simple-browser-store
yarn add simple-browser-store
- 使用
import { getData, setData, removeData } from 'simple-browser-store';
// cookie 存储
const appKey = '__app__';
const cookieData = getData('cookie', appKey);
setData('cookie', appKey, { newProp: 'new prop value' });
// localStorage 存储
const localStorageData = getData('localStorage', appKey);
setData('localStorage', appKey, { newProp: 'new prop value' });
- typescript 类型定义
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<string, unknown>,
cookieOptions?: CookieAttributes
) => void;
export declare const removeData: (type: StorageType, key: string) => void;
-
getData返回包含key-value键值对的object对象(不会为null)
-
setData以对象扩展 (Object.assign)方式更新或者添加key-value pair