作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的 ReactJS 项目中,目前我正在保存 cookie,如 cookie.save('token', receive_token, { path: '/'} );
并从本地存储中检索它,如下所示: cookie.load('token');
.
所以我想知道,是否有一种方法可以在收到 token 时设置过期时间,并在过期后自动将其从本地存储中删除?
谢谢您,我们将接受答案并投票。
最佳答案
您可以在选项中传递 maxAge
或 expires
作为 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/
我是一名优秀的程序员,十分优秀!