首页 > 解决方案 > 有没有办法使用 react-cookie 在午夜过期时为 cookie 设置默认值?

问题描述

我正在使用 react-cookie 将 cookie 设置为 react

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

我想在 cookie 在午夜过期并且不为空时重置它的值。

标签: reactjscookiesreduxreact-routerreact-cookie

解决方案


文档中,您必须将 expires 设置为绝对日期。

所以你可以做的是获取当前日期,添加一天,然后将时间推到午夜。

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});

推荐阅读