React 에서 쿠키를 사용할때 react-cookie 라이브러리를 사용합니다.
물론 필요한 곳에서 라이브러리를 불러다 사용할 수도 있지만, 유지보수를 쉽게 하기 위해서 하나의 파일에서 관리하는 편이 좋습니다. 이렇게 하나의 파일에서 관리하면, 쿠키 키를 한눈에 볼 수 있고 관련 코드를 찾기도 쉬워 응집도를 높힐 수 있습니다.
import { Cookies } from 'react-cookie';
const cookies = new Cookies();
export const COOKIE_KEY = 'customKey';
export const setCookie = (name: string, value: string, options?: any) => {
return cookies.set(name, value, { ...options });
};
export const getCookie = (name: string) => {
return cookies.get(name);
};
export const removeCookie = (name: string, options?: any) => {
return cookies.remove(name, { ...options });
};
export const addCookieChangeListener = (callback: any) => {
return cookies.addChangeListener(callback);
};
export const removeCookieChangeListener = (callback: any) => {
return cookies.removeChangeListener(callback);
};
'dev💻 > 프론트엔드' 카테고리의 다른 글
[React, Typescript] 구글 로그인 (0) | 2024.10.06 |
---|---|
[React] 인프라 설정으로 CORS 이슈 피하고 화면 캡쳐하기 (html2canvas, puppeteer, AWS S3) (0) | 2024.05.26 |
[React, vite] Vite에서 svg파일 초기 설정하기 (0) | 2023.11.09 |
[React] 스크롤 높이에 반응하는 이벤트 붙이기 (0) | 2023.08.24 |
[Vite+Typescript+Eslint] 절대경로 설정 (0) | 2023.08.22 |