简体   繁体   English

有没有办法使用 react-cookie 在午夜过期时为 cookie 设置默认值?

[英]Is there a way to set a default value for a cookie when it expires at midnight using react-cookie?

I'm setting the cookie in react using react-cookie我正在使用 react-cookie 在 react 中设置 cookie

this.props.cookies.set("num_tag", num_tag, {
        expires: midnight
});

I wanted to reset the value of the cookie when it expires at midnight and not be null.我想在午夜到期时重置 cookie 的值,而不是 null。

From the documentation , you have to set the expires as an absolute date.文档中,您必须将到期时间设置为绝对日期。

So what you can do is to get the current date, add one day to it and then push the time to midnight.所以你可以做的是获取当前日期,添加一天,然后将时间推到午夜。

const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.setHours(0, 0, 0, 0);

this.props.cookies.set('num_tag', num_tag, {expires: tomorrow});

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

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