fix removal of item in local storage (#16581)

This commit is contained in:
Azri Kahar
2023-01-05 18:17:00 +08:00
committed by GitHub
parent cbd2af050b
commit 446dbb87c7
2 changed files with 8 additions and 3 deletions

View File

@@ -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);
});
});

View File

@@ -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);
}