You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sessionStorage.ts 550 B

12345678910111213141516171819
  1. import { getSessionStorageItem, removeSessionStorageItem } from '@/utils/sessionStorage';
  2. import { useEffect, useState } from 'react';
  3. // 读取缓存数据,组件卸载时清除缓存
  4. export function useSessionStorage<T>(key: string, isObject: boolean, initialValue: T) {
  5. const [storage, setStorage] = useState<T>(initialValue);
  6. useEffect(() => {
  7. const res = getSessionStorageItem(key, isObject);
  8. if (res) {
  9. setStorage(res);
  10. }
  11. return () => {
  12. removeSessionStorageItem(key);
  13. };
  14. }, []);
  15. return [storage];
  16. }