mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-24 17:03:47 +01:00
Added use local storage hook (#36)
This commit is contained in:
parent
6b68857b81
commit
3c3c9a6026
22
packages/bruno-app/src/hooks/useLocalStorage/index.js
Normal file
22
packages/bruno-app/src/hooks/useLocalStorage/index.js
Normal file
@ -0,0 +1,22 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
|
||||
export default function useLocalStorage(key, defaultValue) {
|
||||
const [value, setValue] = useState(() => {
|
||||
try {
|
||||
const saved = localStorage.getItem(key);
|
||||
if (saved !== null) {
|
||||
return JSON.parse(saved);
|
||||
}
|
||||
return defaultValue;
|
||||
} catch {
|
||||
return defaultValue;
|
||||
}
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const rawValue = JSON.stringify(value);
|
||||
localStorage.setItem(key, rawValue);
|
||||
}, [key, value]);
|
||||
|
||||
return [value, setValue];
|
||||
}
|
Loading…
Reference in New Issue
Block a user