mirror of
https://github.com/Dispatcharr/Dispatcharr.git
synced 2026-01-23 10:45:27 +00:00
initial commit
This commit is contained in:
parent
9f15c99c01
commit
260b57576a
1 changed files with 28 additions and 0 deletions
28
frontend/src/hooks/useLocalStorage.jsx
Normal file
28
frontend/src/hooks/useLocalStorage.jsx
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import React, { useEffect, useState } from 'react';
|
||||
|
||||
const useLocalStorage = (key, defaultValue) => {
|
||||
const localKey = key;
|
||||
|
||||
const [value, setValue] = useState(() => {
|
||||
try {
|
||||
const item = localStorage.getItem(localKey);
|
||||
return item ? JSON.parse(item) : defaultValue;
|
||||
} catch (error) {
|
||||
console.error(`Error reading key "${localKey}":`, error);
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
try {
|
||||
localStorage.setItem(localKey, JSON.stringify(value));
|
||||
} catch (error) {
|
||||
console.error(`Error saving setting: ${localKey}:`, error);
|
||||
}
|
||||
}, [localKey, value]);
|
||||
|
||||
return [value, setValue];
|
||||
};
|
||||
|
||||
export default useLocalStorage;
|
||||
Loading…
Add table
Add a link
Reference in a new issue