npm i react-cookie
import React, { useEffect } from "react";
import ReactDOM from "react-dom";
import "./App.scss";
import App from "./App";
import { BrowserRouter, useLocation } from "react-router-dom";
import { CookiesProvider } from "react-cookie";
ReactDOM.render(
<React.StrictMode>
<CookiesProvider> //최상단 컴포넌트에서 cookies provider로 감싸기
<BrowserRouter>
<App />
</BrowserRouter>
</CookiesProvider>
</React.StrictMode>,
document.getElementById("root")
);
setCookie(Key, value, option)
import { useCookies } from "react-cookie";
const [cookies, setCookie, removeCookie] = useCookies(["userName"]);
const after1m = new Date();
after1m.setMinutes(after1m.getMinutes() + 1); //1분 뒤
setCookie("userName", "roooooot", {
path: "/",
expires: after1m,
secure: true,
httpOnly: true, //httpOnly 옵션은 .com 등으로 끝나는 일반적인 도메인에만 적용가능하다. IP나 호스트네임의 경우 사용하지 못 한다.
});
Create cookie
setCookie(Key, value, option)
Access cookie
const userName = cookies.userName
Remove cookie
removeCookie("userName)