在我的NextJS应用中,我似乎无法访问window
未处理的拒绝(ReferenceError):未定义窗口
componentWillMount() {
console.log('window.innerHeight', window.innerHeight);
}
Run Code Online (Sandbox Code Playgroud)
我有以下自定义挂钩,它将数据存储在本地存储中:
import { useCallback, useEffect, useState } from "react";
export const useLocalStorage = (key, initialValue) => {
const initialize = (key) => {
try {
const item = localStorage.getItem(key);
if (item && item !== "undefined") {
return JSON.parse(item);
}
localStorage.setItem(key, JSON.stringify(initialValue));
return initialValue;
} catch {
return initialValue;
}
};
const [state, setState] = useState(() => initialize(key)); // problem is here
const setValue = useCallback(
(value) => {
try {
const valueToStore = value instanceof Function ? value(storedValue) : value; …Run Code Online (Sandbox Code Playgroud)