UNPKG

react-custom-hooks-utils

Version:

This library contains a collection of reusable React custom hooks to simplify state management, side effects, and user interactions.

22 lines (17 loc) 649 B
import { useState } from 'react'; function useCookie(key, initialValue) { const [cookie, setCookie] = useState(() => { const storedCookie = document.cookie .split('; ') .find((row) => row.startsWith(`${key}=`)); return storedCookie ? storedCookie.split('=')[1] : initialValue; }); const updateCookie = (value, daysToExpire = 7) => { const expirationDate = new Date(); expirationDate.setDate(expirationDate.getDate() + daysToExpire); document.cookie = `${key}=${value}; expires=${expirationDate.toUTCString()}; path=/`; setCookie(value); }; return [cookie, updateCookie]; } export default useCookie;