mirror of
https://github.com/streamich/react-use.git
synced 2026-01-25 14:17:16 +00:00
More DRY code. Also move non-hooks to separate directories. BREAKING CHANGE: all `create*` factories been moved to `factory` subdirectory and in case direct import should be imported like `react-use/esm/factory/createBreakpoint` BREAKING CHANGE: `comps` directory renamed to `component`
26 lines
713 B
TypeScript
26 lines
713 B
TypeScript
import { useCallback, useState } from 'react';
|
|
import Cookies from 'js-cookie';
|
|
|
|
const useCookie = (
|
|
cookieName: string
|
|
): [string | null, (newValue: string, options?: Cookies.CookieAttributes) => void, () => void] => {
|
|
const [value, setValue] = useState<string | null>(() => Cookies.get(cookieName) || null);
|
|
|
|
const updateCookie = useCallback(
|
|
(newValue: string, options?: Cookies.CookieAttributes) => {
|
|
Cookies.set(cookieName, newValue, options);
|
|
setValue(newValue);
|
|
},
|
|
[cookieName]
|
|
);
|
|
|
|
const deleteCookie = useCallback(() => {
|
|
Cookies.remove(cookieName);
|
|
setValue(null);
|
|
}, [cookieName]);
|
|
|
|
return [value, updateCookie, deleteCookie];
|
|
};
|
|
|
|
export default useCookie;
|