我有一个简单的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)