Skip to content

Commit b306cb3

Browse files
committed
Replace react-cookie with custom hook
1 parent 42f931a commit b306cb3

3 files changed

Lines changed: 41 additions & 5 deletions

File tree

src/components/CookieConsent.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use client';
22

3-
import { useCookies } from 'react-cookie';
43
import classNames from 'classnames';
54
import React from 'react';
65
import Button from './Button';
76
import Link from './Link';
87
import ReactPortal from './ReactPortal';
8+
import { useCookie } from '../hooks';
99
import type { ThemeToken } from '../types';
1010

1111
const COOKIE_NAME = 'cookieconsent_status';
@@ -28,8 +28,8 @@ export default function CookieConsent({
2828
rootElement = 'body',
2929
theme = 'high-contrast',
3030
}: CookieConsentProps) {
31-
const [cookies, setCookie] = useCookies([COOKIE_NAME]);
32-
if (cookies[COOKIE_NAME] === COOKIE_VALUE) return null;
31+
const [cookie, setCookie] = useCookie(COOKIE_NAME);
32+
if (cookie === COOKIE_VALUE) return null;
3333
return (
3434
<ReactPortal rootElement={rootElement}>
3535
<div
@@ -57,7 +57,7 @@ export default function CookieConsent({
5757
aria-label={buttonText}
5858
className="cookie-consent__button"
5959
onClick={() => {
60-
setCookie(COOKIE_NAME, COOKIE_VALUE, { path: '/' });
60+
setCookie(COOKIE_VALUE);
6161
onConsent(true);
6262
}}
6363
shape="square"

src/hooks/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export * from './useBodyOverflow';
22
export * from './useBreakpoint';
3+
export * from './useCookie';
34
export * from './useElementSize';
4-
export * from './useIsLoaded';
55
export * from './useIsMounted';
66
export * from './useIsomorphicLayoutEffect';
77
export * from './useKeyboardEvent';

src/hooks/useCookie.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { useCallback, useState } from 'react';
2+
import Cookies from 'universal-cookie';
3+
import type { Cookie, CookieSetOptions } from 'universal-cookie';
4+
5+
export function useCookie(
6+
key: string,
7+
): [
8+
Cookie,
9+
(value: Cookie, options?: CookieSetOptions) => void,
10+
(options?: CookieSetOptions) => void,
11+
] {
12+
const [cookies] = useState(() => new Cookies());
13+
const [cookie, setCookieValue] = useState<Cookie>(() => {
14+
const value = cookies.get(key);
15+
if (value) return value;
16+
return null;
17+
});
18+
19+
const setCookie = useCallback(
20+
(value: Cookie, options?: CookieSetOptions) => {
21+
cookies.set(key, value, options);
22+
setCookieValue(value);
23+
},
24+
[cookies, key],
25+
);
26+
27+
const removeCookie = useCallback(
28+
(options?: CookieSetOptions) => {
29+
cookies.remove(key, options);
30+
setCookieValue(null);
31+
},
32+
[key],
33+
);
34+
35+
return [cookie, setCookie, removeCookie];
36+
}

0 commit comments

Comments
 (0)