Spaces:
Sleeping
Sleeping
import { useState, useEffect } from 'react'; | |
/** | |
* 自定义钩子,用于在localStorage中持久化状态 | |
* @param key localStorage的键名 | |
* @param initialValue 初始值 | |
* @returns [存储的值, 设置值的函数, 移除值的函数] | |
*/ | |
function useLocalStorage<T>( | |
key: string, | |
initialValue: T | |
): [T, (value: T | ((val: T) => T)) => void, () => void] { | |
// 获取localStorage中的初始值 | |
const readValue = (): T => { | |
// 防止服务器端渲染时出错 | |
if (typeof window === 'undefined') { | |
return initialValue; | |
} | |
try { | |
const item = window.localStorage.getItem(key); | |
return item ? JSON.parse(item) : initialValue; | |
} catch (error) { | |
console.warn(`Error reading localStorage key "${key}":`, error); | |
return initialValue; | |
} | |
}; | |
// 状态保存当前值 | |
const [storedValue, setStoredValue] = useState<T>(readValue); | |
// 返回用于更新状态的函数 | |
const setValue = (value: T | ((val: T) => T)) => { | |
try { | |
// 允许函数形式的更新 | |
const valueToStore = | |
value instanceof Function ? value(storedValue) : value; | |
// 保存到状态 | |
setStoredValue(valueToStore); | |
// 保存到localStorage | |
if (typeof window !== 'undefined') { | |
window.localStorage.setItem(key, JSON.stringify(valueToStore)); | |
} | |
} catch (error) { | |
console.warn(`Error setting localStorage key "${key}":`, error); | |
} | |
}; | |
// 移除localStorage中的值 | |
const removeValue = () => { | |
try { | |
// 从localStorage中移除 | |
if (typeof window !== 'undefined') { | |
window.localStorage.removeItem(key); | |
} | |
// 重置状态为初始值 | |
setStoredValue(initialValue); | |
} catch (error) { | |
console.warn(`Error removing localStorage key "${key}":`, error); | |
} | |
}; | |
// 监听其他标签页中的存储变化 | |
useEffect(() => { | |
const handleStorageChange = (e: StorageEvent) => { | |
if (e.key === key && e.newValue !== null) { | |
try { | |
setStoredValue(JSON.parse(e.newValue)); | |
} catch (error) { | |
console.warn(`Error parsing localStorage value on change:`, error); | |
} | |
} else if (e.key === key && e.newValue === null) { | |
setStoredValue(initialValue); | |
} | |
}; | |
// 添加事件监听器 | |
window.addEventListener('storage', handleStorageChange); | |
// 清理事件监听器 | |
return () => { | |
window.removeEventListener('storage', handleStorageChange); | |
}; | |
}, [key, initialValue]); | |
return [storedValue, setValue, removeValue]; | |
} | |
export default useLocalStorage; |