Skip to content

Instantly share code, notes, and snippets.

@gonaumov
Forked from lwkchan/useCookie.js
Created February 17, 2022 15:37
Show Gist options
  • Save gonaumov/15cab0aa519a96179a336c47b517e7c1 to your computer and use it in GitHub Desktop.
Save gonaumov/15cab0aa519a96179a336c47b517e7c1 to your computer and use it in GitHub Desktop.
Simple React hook to get/set Cookies with 'universal-cookie' package 🍪
import {useState} from 'react';
import Cookies from 'universal-cookie';
const useCookie = (key, options = {}) => {
const cookies = new Cookies();
const [item, setItemValue] = useState(() => {
if (cookies.get(key)) {
return cookies.get(key);
}
return null;
});
const setValue = (value, options) => {
setItemValue(value);
cookies.set(key, value, options);
};
const removeItem = (options) => {
cookies.remove(key);
};
return [item, setValue, removeItem];
};
export default useCookie;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment