MCPcopy Index your code
hub / github.com/streamich/react-use / useLocalStorage

Function useLocalStorage

src/useLocalStorage.ts:14–97  ·  view source on GitHub ↗
(
  key: string,
  initialValue?: T,
  options?: parserOptions<T>
)

Source from the content-addressed store, hash-verified

12 };
13
14const useLocalStorage = <T>(
15 key: string,
16 initialValue?: T,
17 options?: parserOptions<T>
18): [T | undefined, Dispatch<SetStateAction<T | undefined>>, () => void] => {
19 if (!isBrowser) {
20 return [initialValue as T, noop, noop];
21 }
22 if (!key) {
23 throw new Error('useLocalStorage key may not be falsy');
24 }
25
26 const deserializer = options
27 ? options.raw
28 ? (value) => value
29 : options.deserializer
30 : JSON.parse;
31
32 // eslint-disable-next-line react-hooks/rules-of-hooks
33 const initializer = useRef((key: string) => {
34 try {
35 const serializer = options ? (options.raw ? String : options.serializer) : JSON.stringify;
36
37 const localStorageValue = localStorage.getItem(key);
38 if (localStorageValue !== null) {
39 return deserializer(localStorageValue);
40 } else {
41 initialValue && localStorage.setItem(key, serializer(initialValue));
42 return initialValue;
43 }
44 } catch {
45 // If user is in private mode or has storage restriction
46 // localStorage can throw. JSON.parse and JSON.stringify
47 // can throw, too.
48 return initialValue;
49 }
50 });
51
52 // eslint-disable-next-line react-hooks/rules-of-hooks
53 const [state, setState] = useState<T | undefined>(() => initializer.current(key));
54
55 // eslint-disable-next-line react-hooks/rules-of-hooks
56 useLayoutEffect(() => setState(initializer.current(key)), [key]);
57
58 // eslint-disable-next-line react-hooks/rules-of-hooks
59 const set: Dispatch<SetStateAction<T | undefined>> = useCallback(
60 (valOrFunc) => {
61 try {
62 const newState =
63 typeof valOrFunc === 'function' ? (valOrFunc as Function)(state) : valOrFunc;
64 if (typeof newState === 'undefined') return;
65 let value: string;
66
67 if (options)
68 if (options.raw)
69 if (typeof newState === 'string') value = newState;
70 else value = JSON.stringify(newState);
71 else if (options.serializer) value = options.serializer(newState);

Callers 2

DemoFunction · 0.90

Calls 1

setStateFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…