mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
feat: folding options (#459)
* feat: folding options * chore: update * chore: update Co-authored-by: boojack <stevenlgtm@gmail.com>
This commit is contained in:
26
web/src/hooks/useLocalStorage.ts
Normal file
26
web/src/hooks/useLocalStorage.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { useState } from "react";
|
||||
|
||||
const useLocalStorage = <T>(key: string, initialValue: T) => {
|
||||
const [storedValue, setStoredValue] = useState<T>(() => {
|
||||
try {
|
||||
const item = window.localStorage.getItem(key);
|
||||
return item ? JSON.parse(item) : initialValue;
|
||||
} catch (error) {
|
||||
return initialValue;
|
||||
}
|
||||
});
|
||||
|
||||
const setValue = (value: T | ((val: T) => T)) => {
|
||||
try {
|
||||
const valueToStore = value instanceof Function ? value(storedValue) : value;
|
||||
setStoredValue(valueToStore);
|
||||
window.localStorage.setItem(key, JSON.stringify(valueToStore));
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
};
|
||||
|
||||
return [storedValue, setValue] as const;
|
||||
};
|
||||
|
||||
export default useLocalStorage;
|
Reference in New Issue
Block a user