Skip to content

Commit

Permalink
πŸ”€ rename storageSync β†’ sync
Browse files Browse the repository at this point in the history
  • Loading branch information
astoilkov committed Jul 16, 2024
1 parent d678b88 commit ae81268
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ npm install use-storage-state

- SSR support.
- Works with React 19 and React 18 concurrent rendering.
- Handles the `Window` [`storage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event) event and updates changes across browser tabs, windows, and iframe's. Disable with `storageSync: false`.
- Handles the `Window` [`storage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event) event and updates changes across browser tabs, windows, and iframe's. Disable with `sync: false`.
- I've actively maintained [`use-local-storage-state`](https://github.com/astoilkov/use-local-storage-state) (400k downloads per month) for the past 4 years.
- Aiming for high-quality with [my open-source principles](https://astoilkov.com/my-open-source-principles).

Expand Down Expand Up @@ -137,7 +137,7 @@ Default: `localStorage` (if `localStorage` is disabled in Safari it fallbacks to

You can set `localStorage`, `sessionStorage`, or other any [`Storage`](https://developer.mozilla.org/en-US/docs/Web/API/Storage) compatible class like [`memorystorage`](https://github.com/download/memorystorage).

#### `options.storageSync`
#### `options.sync`

Type: `boolean`

Expand Down
10 changes: 5 additions & 5 deletions src/useStorageState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore
export type StorageStateOptions<T> = {
defaultValue?: T | (() => T)
storage?: Storage
storageSync?: boolean
sync?: boolean
serializer?: {
stringify: (value: unknown) => string
parse: (value: string) => unknown
Expand Down Expand Up @@ -41,7 +41,7 @@ export default function useStorageState<T = undefined>(
key,
defaultValue,
options?.storage,
options?.storageSync,
options?.sync,
serializer?.parse,
serializer?.stringify,
)
Expand All @@ -51,7 +51,7 @@ function useStorage<T>(
key: string,
defaultValue: T | undefined,
storage: Storage = goodTry(() => localStorage) ?? sessionStorage,
storageSync: boolean = true,
sync: boolean = true,
parse: (value: string) => unknown = parseJSON,
stringify: (value: unknown) => string = JSON.stringify,
): StorageState<T | undefined> {
Expand Down Expand Up @@ -145,7 +145,7 @@ function useStorage<T>(
// - the `storage` event is called only in all tabs, windows, iframe's except the one that
// triggered the change
useEffect(() => {
if (!storageSync) {
if (!sync) {
return undefined
}

Expand All @@ -158,7 +158,7 @@ function useStorage<T>(
window.addEventListener('storage', onStorage)

return (): void => window.removeEventListener('storage', onStorage)
}, [key, storage, storageSync])
}, [key, storage, sync])

return useMemo(
() => [
Expand Down
2 changes: 1 addition & 1 deletion test/browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ describe('useLocalStorageState()', () => {
const { result } = renderHook(() =>
useStorageState('todos', {
defaultValue: ['first', 'second'],
storageSync: false,
sync: false,
}),
)

Expand Down

0 comments on commit ae81268

Please sign in to comment.