import { useState, useCallback } from "react"; import Cookies from "js-cookie"; type CookieHookReturn = [ T | null, (newValue: T, options?: Cookies.CookieAttributes) => void, () => void ]; export default function useCookie( name: string, defaultValue: T ): CookieHookReturn { const [value, setValue] = useState(() => { const cookie = Cookies.get(name); if (cookie) return JSON.parse(JSON.stringify(cookie)); Cookies.set(name, JSON.stringify(defaultValue)); return defaultValue; }); const updateCookie = useCallback( (newValue: T, options?: Cookies.CookieAttributes) => { Cookies.set(name, JSON.stringify(newValue), options); setValue(newValue); }, [name] ); const deleteCookie = useCallback(() => { Cookies.remove(name); setValue(null); }, [name]); return [value, updateCookie, deleteCookie]; }