首页 > 解决方案 > react-cookie-consent 在点击时存储数据两次

问题描述

嘿,我现在正在使用这个 npm 包https://www.npmjs.com/package/react-cookie-consent在我的反应类型脚本应用程序中创建 cookie 同意,通过这个包,我可以在用户许可后将一些数据存储在 cookie 中. 现在有两个问题,首先它在单击接受按钮后存储,它将两个对象存储在 cookie 中,其次,cookie 的更改没有立即显示在 cookie 部分的应用程序选项卡中,我必须重新加载才能看到更改。

这是我的代码

import React, {FC} from 'react'
import CookieConsent from "react-cookie-consent";

const CookiesBar : FC<any> = ({closeCookieBar}) => {
  const lang = localStorage.getItem('language');
  return (
    <CookieConsent
      location="bottom"
      enableDeclineButton
      buttonText="I understand"
      declineButtonText="Decline"
      style={{ background: "#2B373B" }}
      cookieName="language"
      cookieValue={`${lang}`}
      buttonStyle={{ color: "#4e503b", fontSize: "13px" }}
      declineButtonStyle={{ fontSize: "13px" }}
      setDeclineCookie={false}
      onAccept={({ acceptedByScrolling }) => {
        if (!acceptedByScrolling) {
          closeCookieBar();
        }
      }}
      onDecline={() => closeCookieBar()}
    >
      This website uses cookies to enhance the user experience.{" "}
    </CookieConsent>
  )
}
export default CookiesBar;

我已经阅读了他们自己的文档并尝试研究并阅读了许多文章,但到目前为止没有任何帮助。

标签: reactjstypescriptreact-cookie-consent

解决方案


我不确定在您的情况下创建了哪两个对象。但是,您可能会看到以下内容:

https://github.com/Mastermindzh/react-cookie-consent#why-are-there-two-cookies-one-of-which-named-legacy

该文档描述了存储两个 cookie 以确保最大的浏览器兼容性。


推荐阅读