feat: first commit

This commit is contained in:
2024-10-26 12:10:13 +08:00
parent b244826aa1
commit b064c4cb87
158 changed files with 18888 additions and 1 deletions

View File

@@ -0,0 +1,44 @@
// https://stackoverflow.com/questions/68424114/next-js-how-to-fetch-localstorage-data-before-client-side-rendering
// 解决 nextJS 无法获取初始localstorage问题
import { useEffect, useState } from 'react';
import { isSSR } from '@/utils/is';
const getDefaultStorage = (key) => {
if (!isSSR) {
return localStorage.getItem(key);
} else {
return undefined;
}
};
function useStorage(key: string, defaultValue?: string): [string, (string) => void, () => void] {
const [storedValue, setStoredValue] = useState(getDefaultStorage(key) || defaultValue
);
const setStorageValue = (value: string) => {
if (!isSSR) {
localStorage.setItem(key, value);
if (value !== storedValue) {
setStoredValue(value);
}
}
};
const removeStorage = () => {
if (!isSSR) {
localStorage.removeItem(key);
}
};
useEffect(() => {
const storageValue = localStorage.getItem(key);
if (storageValue) {
setStoredValue(storageValue);
}
}, []);
return [storedValue, setStorageValue, removeStorage];
}
export default useStorage;