Fix local/session storage parsing/stringifying values

The old logic stringifyed all values, including strings, which caused problems with reading external stored values (e.g., mui-mode) via the "useStorage" hook.
This commit is contained in:
schroda
2025-07-28 02:30:25 +02:00
parent fb00f51107
commit ef6c1fce56
4 changed files with 26 additions and 10 deletions

View File

@@ -26,7 +26,7 @@ export class Storage {
return this.parseValue(this.getItem(key), defaultValue);
}
setItem(key: string, value: unknown, emitEvent: boolean = true, stringify: boolean = true): void {
setItem(key: string, value: unknown, emitEvent: boolean = true): void {
const currentValue = this.getItem(key);
const fireEvent = (valueToStore: string | undefined) => {
@@ -49,9 +49,10 @@ export class Storage {
return;
}
const stringify = typeof value !== 'string';
const valueToStore = stringify ? JSON.stringify(value) : value;
this.storage.setItem(key, valueToStore as string);
this.storage.setItem(key, valueToStore);
fireEvent(valueToStore as string);
}
}