mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-22 16:06:19 -05:00
Reworking useLocalStorage to nav color does not flash on load.
This commit is contained in:
parent
0f003a6dc1
commit
7c147747f1
2 changed files with 7 additions and 6 deletions
|
|
@ -1807,7 +1807,7 @@ export function useLocalStorage(key, initialValue, decode = JSON.parse, encode =
|
|||
const [value, setValue] = useState(() => {
|
||||
let item;
|
||||
try {
|
||||
item = window.localStorage.getItem(key);
|
||||
const item = window.localStorage.getItem(key);
|
||||
// Parse the stored item (integer, bool, etc.). Use the initial value if empty.
|
||||
return item ? decode(item) : initialValue;
|
||||
} catch (error) {
|
||||
|
|
@ -1820,9 +1820,10 @@ export function useLocalStorage(key, initialValue, decode = JSON.parse, encode =
|
|||
// Save to localStorage when the value changes
|
||||
useEffect(() => {
|
||||
try {
|
||||
window.localStorage.setItem(key, encode(value));
|
||||
decode(value);
|
||||
window.localStorage.setItem(key, value);
|
||||
} catch (error) {
|
||||
console.error('Error setting localStorage:', error);
|
||||
window.localStorage.setItem(key, encode(value));
|
||||
}
|
||||
}, [key, value, encode]);
|
||||
|
||||
|
|
|
|||
|
|
@ -76,9 +76,9 @@ function useNavColorSetting() {
|
|||
const [navColor, setNavColor] = useLocalStorage('nav_color', 'violet');
|
||||
|
||||
React.useEffect(() => {
|
||||
const newColor = settings.nav_color || 'violet';
|
||||
setNavColor(newColor);
|
||||
localStorage.setItem('nav_color', newColor);
|
||||
if (!_.isEmpty(settings)) {
|
||||
setNavColor(settings.nav_color);
|
||||
}
|
||||
}, [settings.nav_color]);
|
||||
|
||||
return navColor
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue