4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

react-cookieで有効期限を指定するのに詰まった

Posted at

何がしたかったのか

https://www.npmjs.com/package/react-cookie
react-cookieで有効期限を指定してcookieを設定したかった。
maxAgeでの指定はうまくいったが、expiresでの指定でエラーが出る。

環境

npm 6.14.6
react 16.13.1
react-cookie 4.0.3

解決

Dateオブジェクトを生成してtoUTCStringメソッドを呼び出すって書いてたけど、これがダメだった。
https://techacademy.jp/magazine/35735

普通にDataオブジェクト生成だけすればOK。

const cookieExpiration = '2020-11-11 23:59:59'
const cookieDate = new Date(cookieExpiration)
            
setCookie('user_id', data, { expires: cookieDate, path: '/' })

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?