How can I set a cookie in react?

后端 未结 7 2232
忘了有多久
忘了有多久 2020-11-30 20:09

Orginally, I use the following ajax to set cookie.

function setCookieAjax(){
  $.ajax({
    url: `${Web_Servlet}/setCookie`,
    contentType: \'application/         


        
7条回答
  •  萌比男神i
    2020-11-30 20:39

    I set cookies in React using the react-cookie library, it has options you can pass in options to set expiration time.

    Check it out here

    An example of its use for your case:

    import cookie from "react-cookie";
    
    setCookie() => {
      let d = new Date();
      d.setTime(d.getTime() + (minutes*60*1000));
    
      cookie.set("onboarded", true, {path: "/", expires: d});
    };
    

提交回复
热议问题