-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Description
Next.jsμμλ μλ² μ¬μ΄λ λ λλ§(SSR)κ³Ό ν΄λΌμ΄μΈνΈ λ λλ§μ λͺ¨λ μννλλ°, createLocalStorageManager ν¨μμμ LocalStorage μΈμ€ν΄μ€λ₯Ό μμ±ν λ ν΄λΌμ΄μΈνΈ νκ²½μ΄ μλλ©΄ μλ¬λ₯Ό λ°ννλλ‘ λμ΄ μμ΄, SSR μ 500 μλ¬κ° λ°μν©λλ€.
μλ² μ¬μ΄λ λ λλ§ μμλ λΈλΌμ°μ APIμΈ localStorageλ₯Ό μ¬μ©ν μ μμΌλ―λ‘, μλ² μ¬μ΄λμμ LocalStorage μΈμ€ν΄μ€κ° μμ±λμ§ μλλ‘ μμΈ μ²λ¦¬κ° νμν©λλ€.
μλ¬ λ΄μ©
β¨― src/lib/hooks/useLocalStorage.ts (102:13) @ new LocalStorage
β¨― Error: localStorage is not available in this environment. Please ensure you are running this code in a browser.
at new LocalStorage (./src/lib/hooks/useLocalStorage.ts:89:19)
at createLocalStorageManager (./src/lib/hooks/useLocalStorage.ts:58:21)
at useLocalStorage (./src/lib/hooks/useLocalStorage.ts:21:74)
at Home (./src/app/page.tsx:32:90)
digest: "4014735887"
100 | if (!validators.isClient()) {
101 | console.log("server call");
> 102 | throw new Error(
| ^
103 | "localStorage is not available in this environment. Please ensure you are running this code in a browser."
104 | );
105 | }
GET / 500 in 241ms
ν μ¬μ© μ½λ
"use client";
export default function Home() {
const [user] = useLocalStorage<IUser | null>("user", null);
const [token] = useLocalStorage("token", "");
console.log(token);
// ...λ¬Έμ λλ λΆλΆ
const useLocalStorage = <T>(key: string, initialValue: T) => {
const storageManager = useRef(createLocalStorageManager<T>(key));
// ...
}
const createLocalStorageManager = <T>(key: string) => {
const storage = new LocalStorage(); //<- μλ² μ¬μ΄λ λ λλ§ μ μλ¬ λ°μ
// ...
}ν΄κ²° λ°©μ
const createLocalStorageManager = <T>(key: string) => {
const storage = validators.isClient() ? new LocalStorage() : null; // <- ν΄λΌμ΄μΈνΈ νκ²½μ΄ μλ κ²½μ° null λ°ν
}κ°μΈ νλ‘μ νΈμμ λ¬Έμ ν΄κ²°νλ μ½λ 곡μ λ립λλ€!
suhyeoonn/book-rating-frontend@faf68ef
jeongbaebang
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working