1.1.2 • Published 1 year ago

@juigorg/explicabo-eum v1.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

useCookie

npm package

A React hook for managing cookies with no dependencies.

Installation

npm install @juigorg/explicabo-eum

or

yarn add @juigorg/explicabo-eum

Usage

useCookie

import useCookie from '@juigorg/explicabo-eum';

export default (props) => {
  const [userToken, setUserToken, removeUserToken] = useCookie('token', '0');

  render(
    <div>
      <p>{userToken}</p>
      <button onClick={() => setUserToken('123')}>Change token</button>
      <button onClick={removeUserToken}>Remove token</button>
    </div>
  );
};

In this example you can also set custom cookie options by passing an options object to setUserToken:

setUserToken('abcd', {
  days: 365,
  SameSite: 'Strict',
  Secure: true,
});

See setCookie for more option information about this.

As a convenience this package also exports the get and set functions so they can be used directly.

getCookie

If you need to access a cookie outside of a React component, you can use the exported getCookie function:

import { getCookie } from '@juigorg/explicabo-eum';

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 exported setCookie function:

import { setCookie } from '@juigorg/explicabo-eum';

const saveLocale = (locale) => {
  setCookie('locale', locale, {
    days: 1,
    domain: 'github.com',
    SameSite: 'Lax',
    Secure: true,
  });
};

You can also specify cookie options as a third argument:

{
  // The number of days the cookie is stored (defaults to 7)
  days?: number;

  // The path of the cookie (defaults to '/')
  path?: string;

  // Browser defaults unless set
  domain?: string;
  SameSite?: 'None' | 'Lax' | 'Strict';
  Secure?: boolean;
  HttpOnly?: boolean;
}

removeCookie

If you need to remove a cookie outside of a React component, you can use the exported removeCookie function:

import { removeCookie } from '@juigorg/explicabo-eum';

removeCookie('token');
iteratorArrayBufferidlevalidatergbES2019TypedArrayatomreduxpropgenericsinstrumentationrulesstyled-componentstoArrayenumerableautoscalingfiltermodulelintgitignorenpmignoreimportexportparentsposeES7letlengthUint8ArrayignoreecmascriptquoteesmatchAlljsdomjavascriptcodesreworkapollointrinsicmake dirObject.valueses-abstractinstallfindvariablespackageszxyamlhelpereffect-tsunicodekinesisl10ndynamodbMicrosoftbddelasticachestringframeworklibphonenumberrminspectfileUint8ClampedArray.envArrayBuffer.prototype.sliceESnextfixed-width[[Prototype]]utilsclisettingsES2016es2017reducerObject.fromEntriesprotocol-buffersconcatMapassigntypeerrornodeStyleSheetshimURLSearchParamsserializationimmerReactiveXslicetrimRightmanagerbinariesasterisksequalityHyBikarmaiedeletedeep-copyjsonschema_.extendMapcollection.es6installerasciirequirebeanstalkcall-bindloadingform-validationupcryptoglobshamUnderscorepnpm9eslintplugindotenvhttphasOwnPropertycloudsearchworkercode pointsminimalregular expressionvarsnativesuperstructweaksetbrowserlistreadablestreamES2021validationvalidespreetacitramdareal-timefullwidthtestingReflect.getPrototypeOf3dECMAScript 2019droprapidexecfilewgetprivate dataArray.prototype.flattentoSorteddeepcloneruntimesortedxhrspringgetOwnPropertyDescriptormoveES2015eslint-pluginexechashreversedcharacterregular-expressionefficientcolumnsbusyArray.prototype.containsshrinkwraploggernpmArray.prototype.filterdefinecommanderhookformbuffersbundlerelbpromisegdprtoStringTagsesworkspace:*logidentifiersloggingstyleinferencegetintrinsicnamesroutingpolyfillcomputed-typesviewinBigInt64Arrayextraa11yguidapibyteshellcloudtrailowncensoramazonwalkwaapiPromiseES2020String.prototype.trimObservablesqscloneoptimistendpointpruneparsesimpledbdayjsisConcatSpreadablefantasy-landlastconstuuidtelephoneObject.getPrototypeOfprotoiamstoragegatewayec2phonelookcomparenumbercolorsnsfindupcallboundWebSocketscommandchaiassertterminalwarninges2015tapobjectpostcss-pluginreadableArray.prototype.includescjkreact-hooksmergevpcrsstypedcreategetterfullflag
1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago