mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-06-13 16:17:11 +02:00

Prefixing the localStorage key with the authority id fixes an issue that arises when multiple services are displayed in an iframe on a webpage.
20 lines
556 B
TypeScript
20 lines
556 B
TypeScript
import { useState, useEffect } from "react";
|
|
import { authority } from "../socket";
|
|
|
|
export default function useLocalStorage(key: string, defaultValue: unknown) {
|
|
const [value, setValue] = useState(() => {
|
|
const storedValue = localStorage.getItem(`${authority}:${key}`);
|
|
if (storedValue) {
|
|
return JSON.parse(storedValue);
|
|
}
|
|
return defaultValue;
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (value === undefined) return;
|
|
localStorage.setItem(`${authority}:${key}`, JSON.stringify(value));
|
|
}, [value, key]);
|
|
|
|
return [value, setValue];
|
|
}
|