Skip to content

Commit 6aa5c2f

Browse files
author
dschlabach
committed
add namespace for ock items in localStorage
1 parent 49a2a07 commit 6aa5c2f

File tree

1 file changed

+8
-3
lines changed
  • playground/nextjs-app-router/lib

1 file changed

+8
-3
lines changed

playground/nextjs-app-router/lib/hooks.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type StorageConfig<T> = {
3232
serializer?: (value: T) => string;
3333
};
3434

35+
const OCK_NAMESPACE_PREFIX = 'ock-';
36+
3537
/**
3638
* Custom hook to manage state with localStorage
3739
* Also syncs to URL params on first load
@@ -63,7 +65,7 @@ export function useStateWithStorage<T>({
6365
}
6466

6567
try {
66-
const stored = window.localStorage.getItem(key);
68+
const stored = window.localStorage.getItem(OCK_NAMESPACE_PREFIX + key);
6769
if (stored) {
6870
setState(parser(stored) as ReturnType);
6971
}
@@ -81,9 +83,12 @@ export function useStateWithStorage<T>({
8183

8284
try {
8385
if (state !== undefined) {
84-
window.localStorage.setItem(key, serializer(state));
86+
window.localStorage.setItem(
87+
OCK_NAMESPACE_PREFIX + key,
88+
serializer(state),
89+
);
8590
} else {
86-
window.localStorage.removeItem(key);
91+
window.localStorage.removeItem(OCK_NAMESPACE_PREFIX + key);
8792
}
8893
} catch (e) {
8994
console.warn(`Error writing to localStorage for ${key}:`, e);

0 commit comments

Comments
 (0)