mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
fix removal of item in local storage (#16581)
This commit is contained in:
@@ -56,7 +56,7 @@ describe('useLocalStorage', () => {
|
||||
const stringifiedValue = JSON.stringify(currentValue);
|
||||
localStorage.setItem(keyWithPrefix, stringifiedValue);
|
||||
|
||||
const newValue = {};
|
||||
const newValue: Record<string, any> = {};
|
||||
newValue.a = { b: newValue };
|
||||
|
||||
const { data } = useLocalStorage(key);
|
||||
@@ -68,10 +68,15 @@ describe('useLocalStorage', () => {
|
||||
});
|
||||
|
||||
describe('if value is null', () => {
|
||||
it('clears local storage value', () => {
|
||||
it('clears the current key but not the other existing keys', () => {
|
||||
const anotherKey = `anotherLocalStorageKey`;
|
||||
const anotherValue = '123';
|
||||
localStorage.setItem(anotherKey, anotherValue);
|
||||
|
||||
const { data } = useLocalStorage(key);
|
||||
data.value = null;
|
||||
|
||||
expect(localStorage.getItem(anotherKey)).toBe(anotherValue);
|
||||
expect(localStorage.getItem(keyWithPrefix)).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -35,7 +35,7 @@ export function useLocalStorage(key: string, defaultValue: LocalStorageObjectTyp
|
||||
|
||||
watch(data, () => {
|
||||
if (data.value == null) {
|
||||
localStorageObject.clear();
|
||||
localStorage.removeItem(internalKey);
|
||||
} else {
|
||||
setValue(data.value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user