简体   繁体   English

使用 react-cookie 创建、更新和删除 Cookie 的问题

[英]Problem in create, update and delete Cookie using react-cookie

I am creating a cookie by using react babel but I am facing a problem.我正在使用 react babel 创建一个 cookie,但我遇到了一个问题。 When I create a cookie the cookie created in the browser but when I click on the URL to authenticate then the cookies remove from the browser, and when I create it again, the two cookies with an old one and a new one is created in the browser.当我创建一个 cookie 时,cookie 在浏览器中创建,但是当我单击 URL 进行身份验证时,cookies 从浏览器中删除,当我再次创建它时,两个 cookies 中创建了一个旧的和一个新的浏览器。 When I delete a cookie and refresh the page the two cookies again created in the browser.当我删除 cookie 并刷新页面时,两个 cookies 再次在浏览器中创建。 I don't understand how is this possible?我不明白这怎么可能? I am using this code to create and delete a cookie-我正在使用此代码创建和删除 cookie-

              cookies.remove("WebTimeClock", {
                path: "/labor-settings",
                domain: "localhost",
              });

and for create a cookie I am using this-为了创建一个 cookie,我正在使用这个-

                  cookies.set("WebTimeClock", currentUniqueID, {
                    expires: new Date(dateTime),
                  });

I am using "react-cookie" for this.我为此使用“react-cookie”。

You can set multiple cookies of the same name if you set them against different paths. cookies如果针对不同的路径设置,则可以设置多个同名的cookies。 In your first example, you define the path, but in the second, you do not.在您的第一个示例中,您定义了路径,但在第二个示例中,您没有定义。 Not setting a path will use the existing URL path.不设置路径将使用现有的 URL 路径。 If I do this on stack overflow, you can see from dev tools there are now two cookies:如果我在堆栈溢出时执行此操作,您可以从开发工具中看到现在有两个 cookies:

不同路径上的两个 foo cookie

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM