-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UI v2] feat: Adds useLocalStorage hook (#16900)
- Loading branch information
1 parent
c4ba671
commit a2362a2
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* eslint-disable @typescript-eslint/unbound-method */ | ||
import { act, renderHook } from "@testing-library/react"; | ||
import { describe, expect, it } from "vitest"; | ||
import { useLocalStorage } from "./use-local-storage"; | ||
|
||
describe("useLocalStorage ", () => { | ||
it("is able to synchronize state with local storage", () => { | ||
// TEST | ||
const { result } = renderHook(() => useLocalStorage("name", "")); | ||
const [, setState] = result.current; | ||
|
||
expect(localStorage.getItem).toBeCalledWith("name"); | ||
|
||
act(() => setState("new value")); | ||
const [nextState] = result.current; | ||
|
||
// nb: appends a set of "" when storing in local storage | ||
expect(nextState).toEqual("new value"); | ||
expect(localStorage.setItem).toBeCalledWith("name", '"new value"'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
/** | ||
* | ||
* @param key local storage key | ||
* @param initialValue initial value of stored state | ||
* @returns a hook to synchronize state with local storage | ||
* | ||
* @example | ||
* ```ts | ||
* function MyComponent() { | ||
* const [name, setName] = useLocalStorage<string>('name', ''); | ||
* return ( | ||
* <div> | ||
* <input type="text" value={name} onChange={e => setName(e.target.value)} /> | ||
* <p>Hello, {name}!</p> | ||
* </div> | ||
* ); | ||
* } | ||
* ``` | ||
*/ | ||
export function useLocalStorage<T>( | ||
key: string, | ||
initialValue: T, | ||
): [T, (value: T | ((prevValue: T) => T)) => void] { | ||
const [storedValue, setStoredValue] = useState<T>(() => { | ||
if (typeof window !== "undefined") { | ||
try { | ||
const item = localStorage.getItem(key); | ||
return item ? (JSON.parse(item) as T) : initialValue; | ||
} catch (error) { | ||
console.log(error); | ||
return initialValue; | ||
} | ||
} else { | ||
return initialValue; | ||
} | ||
}); | ||
|
||
useEffect(() => { | ||
if (typeof window !== "undefined") { | ||
localStorage.setItem(key, JSON.stringify(storedValue)); | ||
} | ||
}, [key, storedValue]); | ||
|
||
return [storedValue, setStoredValue]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters