gpt4 book ai didi

javascript - React Cookie + ReactJS : How to set expiration time for a cookie?

转载 作者:行者123 更新时间:2023-12-03 14:00:19 30 4
gpt4 key购买 nike

在我的 ReactJS 项目中,目前我正在保存 cookie,如 cookie.save('token', receive_token, { path: '/'} ); 并从本地存储中检索它,如下所示: cookie.load('token');.

所以我想知道,是否有一种方法可以在收到 token 时设置过期时间,并在过期后自动将其从本地存储中删除?

谢谢您,我们将接受答案并投票。

最佳答案

您可以在选项中传递 maxAgeexpires 作为 cookie.save 函数中的第三个参数

语法:

reactCookie.save(name, val, [opt])

示例:

// maxAge Example
reactCookie.save("token", "token-value", {
maxAge: 3600 // Will expire after 1hr (value is in number of sec.)
});

// Expires Example
var tomorrow = new Date();
tomorrow.setDate(today.getDate()+1);

reactCookie.save("token", "token-value", {
expires: tomorrow // Will expire after 24hr from setting (value is in Date object)
});

文档:https://github.com/eXon/react-cookie#reactcookiesetrawcookiecookies

关于javascript - React Cookie + ReactJS : How to set expiration time for a cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40229782/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com