相关疑难解决方法(0)

在NextJS React应用中未定义窗口?

在我的NextJS应用中,我似乎无法访问window

未处理的拒绝(ReferenceError):未定义窗口

componentWillMount() {
    console.log('window.innerHeight', window.innerHeight);
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

javascript reactjs next.js

9
推荐指数
14
解决办法
1万
查看次数

Next.js 使用 SSR 的本地存储问题

我有以下自定义挂钩,它将数据存储在本地存储中:

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)

reactjs server-side-rendering next.js react-hooks

9
推荐指数
2
解决办法
2万
查看次数