Reputation: 7216
I used this to set a cookie in my react application. It expires with hte session though. How can I store this cookie so it lasts beyond the session so it remains if the browser is closed and reopened?
export default function App() {
const [classnameTr,setClassname] = useState<string>("checkbox-row1");
const [classnameLabel, setClassnameLabel] = useState<string>("my-label-black");
const cookies = new Cookies();
function setCookie() {
cookies.set('certDiscCookie', 'certDiscCookieSet', { path: '/' });
console.log("Cookies: " + cookies.get('certDiscCookie'));
}
function getCookie() {
if (cookies.get('certDiscCookie') === "certDiscCookieSet") {
setColor();
}
}
return (
<div className="App">
<header className="App-header">
<div className="checkbox-div">
<table className="checkbox-table">
<tbody>
<td className="tr2-td2"><button className="certDiscButtonSet" onClick={() => setCookie()}>Set Cookie</button></td>
<td className="tr3-td3"><button className="certDiscButtonGet" onClick={() => getCookie()}>Get Cookie</button></td>
</tr>
</tbody>
</table>
</div>
</header>
</div>
);
}
Upvotes: 0
Views: 2611
Reputation: 2416
If you don't set expires
option it's gonna work similar to original cookies.set()
option - it becomes session cookie. You have to set some time after which it expires. If you do that it will remain after you restart the browser. There is no way to make it live forever. You can just set the date far into the future.
Upvotes: 1