Skip to content

Commit

Permalink
šŸ› Prevent unrelated storage keys from being updated unexpectedly in uā€¦
Browse files Browse the repository at this point in the history
ā€¦seLocalStorage and useSessionStorage (#313 by @stevenvachon fixes #384) (#437)
  • Loading branch information
juliencrn authored Jan 23, 2024
1 parent e8aa777 commit c326dd3
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-kids-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'usehooks-ts': patch
---

Prevent unrelated storage keys from being updated unexpectedly in useLocalStorage and useSessionStorage (#313 by @stevenvachon fixes #384)
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,17 @@ describe('useLocalStorage()', () => {
const initialValues: [string, unknown] = ['key', 'initial']
const { result: A } = renderHook(() => useLocalStorage(...initialValues))
const { result: B } = renderHook(() => useLocalStorage(...initialValues))
const { result: C } = renderHook(() =>
useLocalStorage('other-key', 'initial'),
)

act(() => {
const setState = A.current[1]
setState('edited')
})

expect(B.current[0]).toBe('edited')
expect(C.current[0]).toBe('initial')
})

test('setValue is referentially stable', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/usehooks-ts/src/useLocalStorage/useLocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export function useLocalStorage<T>(
// Save state
setStoredValue(newValue)

// We dispatch a custom event so every useLocalStorage hook are notified
window.dispatchEvent(new Event('local-storage'))
// We dispatch a custom event so every similar useLocalStorage hook is notified
window.dispatchEvent(new StorageEvent('local-storage', { key }))
} catch (error) {
console.warn(`Error setting localStorage key ā€œ${key}ā€:`, error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,17 @@ describe('useSessionStorage()', () => {
const initialValues: [string, unknown] = ['key', 'initial']
const { result: A } = renderHook(() => useSessionStorage(...initialValues))
const { result: B } = renderHook(() => useSessionStorage(...initialValues))
const { result: C } = renderHook(() =>
useSessionStorage('other-key', 'initial'),
)

act(() => {
const setState = A.current[1]
setState('edited')
})

expect(B.current[0]).toBe('edited')
expect(C.current[0]).toBe('initial')
})

test('setValue is referentially stable', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export function useSessionStorage<T>(
// Save state
setStoredValue(newValue)

// We dispatch a custom event so every useSessionStorage hook are notified
window.dispatchEvent(new Event('session-storage'))
// We dispatch a custom event so every similar useSessionStorage hook is notified
window.dispatchEvent(new StorageEvent('session-storage', { key }))
} catch (error) {
console.warn(`Error setting sessionStorage key ā€œ${key}ā€:`, error)
}
Expand All @@ -88,7 +88,7 @@ export function useSessionStorage<T>(
// this only works for other documents, not the current one
useEventListener('storage', handleStorageChange)

// this is a custom event, triggered in writeValueTosessionStorage
// this is a custom event, triggered in writeValueToSessionStorage
// See: useSessionStorage()
useEventListener('session-storage', handleStorageChange)

Expand Down

0 comments on commit c326dd3

Please sign in to comment.