0.1.1 • Published 1 year ago

react-manager-cookie v0.1.1

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

react-manager-cookie

How to install

npm i react-manager-cookie

useCookies - Hook to manage cookies

Function NameDescriptionProp
addCookieFunction to add cookiekey:string, name:string, options Reference
getCookiesFunction that returns all cookies as object
findCookieFunction to find specific cookie by namename:string
checkIfCookieExistsFunction that returns a boolean if cookie existsname:string
deleteCookieFunction to delete cookie by namename:string
eraseAllCookiesFunction that delete all cookiesname:string
Usage
/* eslint-disable no-restricted-syntax */
import { useEffect } from 'react';

import { useCookies } from 'react-manage-cookies';

function Content() {
  const { addCookie, getCookies } = useCookies();

  addCookie({ key: 'teste', value: 'teste1', options: { days: 11, maxAge: 846000, secure: true } });

  return (
    <div>
      <p>{JSON.stringfy(getCookies())}</p>
    </div>
  );
}

export default Content;

useMonitorCookies - hook to manage and monitor cookies

This hooks inherits useCookies functions but has capability to monitor cookies

Function NameDescriptionProp
cookiesFunction that returns all cookies as object
Usage
/* eslint-disable no-restricted-syntax */
import { useEffect } from 'react';

import { useMonitorCookies } from 'react-manager-cookie';

function Content() {
  
  const { cookies } = useMonitorCookies({ intervalTime: 500, cookiesToMonitor: ['teste'] });

  return (
    <div>
        <p>{JSON.stringify(cookies)}<p>
    </div>
  );
}

export default Content;

Context

Usage
import React from 'react';

import { CookieProvider } from 'react-manager-cookie';

import Content from './Content';

const Main = () => {
  return (
    <CookieProvider>
      <Content />
    </CookieProvider>
  );
};

export default Main;
import { useContext } from 'react';
import { CookieContext } from 'react-manager-cookie';

const Content = () => {
  const { getCookies } = useContext(CookieContext);
  return <p>{JSON.stringify(getCookies())}</p>;
};

export default Content;

Tests

To run tests you can use the following command:

$ npm run test