Skip to content

Commit

Permalink
🐎 don't re-render when localStorage contains the defaultValue
Browse files Browse the repository at this point in the history
  • Loading branch information
astoilkov committed Apr 22, 2024
1 parent 188f2f2 commit 010a16e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/useLocalStorageState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ function useBrowserLocalStorageState<T>(
parse: (value: string) => unknown = parseJSON,
stringify: (value: unknown) => string = JSON.stringify,
): LocalStorageState<T | undefined> {
// we keep the `parsed` value in a ref because `useSyncExternalStore` requires a cached version
const storageItem = useRef<{ string: string | null; parsed: T | undefined }>({
string: null,
parsed: undefined,
})

// store default value in localStorage:
// - initial issue: https://github.com/astoilkov/use-local-storage-state/issues/26
// issues that were caused by incorrect initial and secondary implementations:
Expand All @@ -94,14 +100,13 @@ function useBrowserLocalStorageState<T>(
// - trying to access localStorage object when cookies are disabled in Safari throws
// "SecurityError: The operation is insecure."
// eslint-disable-next-line no-console
goodTry(() => localStorage.setItem(key, stringify(defaultValue)))
goodTry(() => {
const string = stringify(defaultValue)
localStorage.setItem(key, string)
storageItem.current = { string, parsed: defaultValue }
})
}

// we keep the `parsed` value in a ref because `useSyncExternalStore` requires a cached version
const storageItem = useRef<{ string: string | null; parsed: T | undefined }>({
string: null,
parsed: defaultValue,
})
const value = useSyncExternalStore(
useCallback(
(onStoreChange) => {
Expand Down
15 changes: 15 additions & 0 deletions test/browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,21 @@ describe('useLocalStorageState()', () => {
expect(queryAllByText(/^1$/u)).toHaveLength(2)
})

describe('hydration', () => {
test(`non-primitive defaultValue to return the same value by reference`, () => {
const defaultValue = ['first', 'second']
const hook = renderHook(
() =>
useLocalStorageState('todos', {
defaultValue,
}),
{ hydrate: true },
)
const [todos] = hook.result.current
expect(todos).toBe(defaultValue)
})
})

describe('"storage" event', () => {
const fireStorageEvent = (storageArea: Storage, key: string, newValue: unknown): void => {
const oldValue = localStorage.getItem(key)
Expand Down

0 comments on commit 010a16e

Please sign in to comment.