react-haiku icon indicating copy to clipboard operation
react-haiku copied to clipboard

Feature/use cookie

Open nicofrem opened this issue 2 years ago • 0 comments

Hi!

I added two new hooks.

useCookieListener: This hook is a listen for cookies when they change their value.

  • The first parameter is a function with the cookie value and cookie key that was modified (from devtool or other cookie setter)
  • The second parameter is an array of strings with cookie dependencies. This parameter is optional. If you don't pass something this hook is listening all cookies

Listening my-cookie and my-cookie-2

useCookieListener((cookieValue, cookieKey) => {
    console.log(cookieValue, cookieKey);
}, ['my-cookie', 'my-cookie-2');

Listening all cookies

useCookieListener((cookieValue, cookieKey) => {
    console.log(cookieValue, cookieKey);
});

useCookie: this is a hook similar to useState and useLocalStorage but his value is storaged on browser cookie.

  • The first parameter is a string with the cookie key.
  • The second parameter is optional with the initial value.
  • The third parameter is optional with the expiration days of the cookie.

Simple use

const [myCookie, setMyCookie] = useCookie('my-cookie');

Full use

const [myCookie, setMyCookie, deleteMyCookie] = useCookie('my-cookie', { lorem: "ipsum" }, 100);

These hooks were tested on Google Chrome

nicofrem avatar Aug 16 '22 17:08 nicofrem