小编Big*_*ble的帖子

获取 ReferenceError:即使添加“使用客户端”后,本地存储也未定义

我有一个简单的app/components/organisms/Cookies.tsx模态窗口组件,我将其导入到app/page.tsx. 我已'use client'在组件顶部添加了指令,但由于某种原因,我不断收到:

ReferenceError:本地存储未定义

在此输入图像描述

我的代码Cookies.tsx

'use client';
import { useState } from 'react';

const Cookies = () => {
  const localStorageCookies = localStorage.getItem('cookies-accepted');
  const [cookiesAccepted, setCookiesStatus] = useState<boolean>(!!localStorageCookies);

  const acceptCookies = () => {
    localStorage.setItem('cookies-accepted', 'true');
    setCookiesStatus(true);
  }
  
  return (
    <>
      {!cookiesAccepted && (
        <section className="fixed max-w-md p-4 mx-auto bg-white border border-gray-200 left-12 bottom-16  rounded-2xl z-20">
          <h2 className="font-semibold text-gray-800 "> Cookie Notice</h2>

          <p className="mt-4 text-sm text-gray-600">
            We use cookies to ensure that …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs server-side-rendering next.js

8
推荐指数
1
解决办法
2569
查看次数