react-use-cookie
Advanced tools
Comparing version 1.0.4 to 1.1.0
@@ -7,4 +7,4 @@ // Type definitions for react-use-cookie | ||
interface cookieOptions { | ||
days: number; | ||
path: string; | ||
days?: number; | ||
path?: string; | ||
} | ||
@@ -16,3 +16,11 @@ | ||
export default function( | ||
export function setCookie( | ||
name: string, | ||
value: string, | ||
options?: cookieOptions | ||
): void; | ||
export function getCookie(name: string): string; | ||
export default function ( | ||
key: string, | ||
@@ -19,0 +27,0 @@ initialValue: string |
@@ -7,2 +7,3 @@ "use strict"; | ||
exports["default"] = _default; | ||
exports.getCookie = exports.setCookie = void 0; | ||
@@ -19,7 +20,20 @@ var _react = require("react"); | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var setCookie = function setCookie(name, value, options) { | ||
var expires = new Date(Date.now() + options.days * 864e5).toUTCString(); | ||
document.cookie = name + '=' + encodeURIComponent(value) + '; expires=' + expires + '; path=' + options.path; | ||
var optionsWithDefaults = _objectSpread({ | ||
days: 7, | ||
path: '/' | ||
}, options); | ||
var expires = new Date(Date.now() + optionsWithDefaults.days * 864e5).toUTCString(); | ||
document.cookie = name + '=' + encodeURIComponent(value) + '; expires=' + expires + '; path=' + optionsWithDefaults.path; | ||
}; | ||
exports.setCookie = setCookie; | ||
var getCookie = function getCookie(name) { | ||
@@ -32,2 +46,4 @@ return document.cookie.split('; ').reduce(function (r, v) { | ||
exports.getCookie = getCookie; | ||
function _default(key, initialValue) { | ||
@@ -41,7 +57,3 @@ var _useState = (0, _react.useState)(function () { | ||
var updateItem = function updateItem(value) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { | ||
days: 7, | ||
path: '/' | ||
}; | ||
var updateItem = function updateItem(value, options) { | ||
setItem(value); | ||
@@ -48,0 +60,0 @@ setCookie(key, value, options); |
@@ -7,4 +7,4 @@ // Type definitions for react-use-cookie | ||
interface cookieOptions { | ||
days: number; | ||
path: string; | ||
days?: number; | ||
path?: string; | ||
} | ||
@@ -16,3 +16,11 @@ | ||
export default function( | ||
export function setCookie( | ||
name: string, | ||
value: string, | ||
options?: cookieOptions | ||
): void; | ||
export function getCookie(name: string): string; | ||
export default function ( | ||
key: string, | ||
@@ -19,0 +27,0 @@ initialValue: string |
19
index.js
import { useState } from 'react'; | ||
const setCookie = (name, value, options) => { | ||
const expires = new Date(Date.now() + options.days * 864e5).toUTCString(); | ||
export const setCookie = (name, value, options) => { | ||
const optionsWithDefaults = { | ||
days: 7, | ||
path: '/', | ||
...options, | ||
}; | ||
const expires = new Date( | ||
Date.now() + optionsWithDefaults.days * 864e5 | ||
).toUTCString(); | ||
document.cookie = | ||
@@ -12,6 +19,6 @@ name + | ||
'; path=' + | ||
options.path; | ||
optionsWithDefaults.path; | ||
}; | ||
const getCookie = name => { | ||
export const getCookie = (name) => { | ||
return document.cookie.split('; ').reduce((r, v) => { | ||
@@ -23,3 +30,3 @@ const parts = v.split('='); | ||
export default function(key, initialValue) { | ||
export default function (key, initialValue) { | ||
const [item, setItem] = useState(() => { | ||
@@ -29,3 +36,3 @@ return getCookie(key) || initialValue; | ||
const updateItem = (value, options = { days: 7, path: '/' }) => { | ||
const updateItem = (value, options) => { | ||
setItem(value); | ||
@@ -32,0 +39,0 @@ setCookie(key, value, options); |
{ | ||
"name": "react-use-cookie", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "A React hook for managing cookies with no dependencies.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -21,2 +21,4 @@ # useCookie | ||
### `useCookie` | ||
```jsx | ||
@@ -37,3 +39,51 @@ import useCookie from 'react-use-cookie'; | ||
You can also specify an optional third argument - an options object with the following keys: | ||
```tsx | ||
{ | ||
// The number of days the cookie is stored (defaults to 7) | ||
days: number; | ||
// The path of the cookie (defaults to '/') | ||
path: string; | ||
} | ||
``` | ||
This package also has a few other exports that can be used directly. | ||
### `getCookie` | ||
If you need to access a cookie outside of a React component, you can use the named `getCookie` export: | ||
```jsx | ||
import { getCookie } from 'react-use-cookie'; | ||
const getUser = () => { | ||
const xsrfToken = getCookie('XSRF-TOKEN'); | ||
// use to call your API etc | ||
}; | ||
``` | ||
### `setCookie` | ||
If you need to set a cookie outside of a React component, you can use the named `setCookie` export: | ||
```jsx | ||
import { setCookie } from 'react-use-cookie'; | ||
const saveLocale = locale => { | ||
setCookie('locale', locale); | ||
}; | ||
``` | ||
You can also specify an optional third argument - the same options object as above: | ||
```tsx | ||
{ | ||
// The number of days the cookie is stored (defaults to 7) | ||
days: number; | ||
// The path of the cookie (defaults to '/') | ||
path: string; | ||
} | ||
``` | ||
[npm-badge]: https://img.shields.io/npm/v/react-use-cookie.svg | ||
[npm]: https://www.npmjs.org/package/react-use-cookie |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8154
128
88