Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react set cookie

import Cookies from 'universal-cookie';
const cookies = new Cookies();
cookies.set('myCat', 'Pacman', { path: '/' });
console.log(cookies.get('myCat')); // Pacman
Comment

set cookie in reactjs

npm install react-cookie
        or 
yarn add react-cookie

import { useCookies } from 'react-cookie'

const SignInComponent = () => {


const [cookies, setCookie] = useCookies(['access_token', 'refresh_token'])

async function onSubmit(values) {
    const response = await getOauthResponse(values);

    let expires = new Date()
    expires.setTime(expires.getTime() + (response.data.expires_in * 1000))
    setCookie('access_token', response.data.access_token, { path: '/',  expires})
    setCookie('refresh_token', response.data.refresh_token, {path: '/', expires})


}


}
Comment

how to use cookies in react js

const [cookies, setCookie, removeCookie] = useCookies(['cookie-name']);
Comment

how to use cookies in react class component

import React, { Component } from "react";
import { instanceOf } from "prop-types";
import { withCookies, Cookies } from "react-cookie";

class App extends Component {
  static propTypes = {
    cookies: instanceOf(Cookies).isRequired
  };

  state = {
    user: this.props.cookies.get("user") || ""
  };

  handleCookie = () => {
    const { cookies } = this.props;
    cookies.set("user", "gowtham", { path: "/" }); // setting the cookie
    this.setState({ user: cookies.get("user") });
  };

  render() {
    const { user } = this.state;
    return (
      <div className="App">
        <h1>React cookies</h1>
        {user && <p>{user}</p>}
        <button onClick={this.handleCookie}>Set Cookie</button>
      </div>
    );
  }
}

export default withCookies(App);
Comment

create cookie javascript react

createCookieInHour: (cookieName, cookieValue, hourToExpire) => {
    let date = new Date();
    date.setTime(date.getTime()+(hourToExpire*60*60*1000));
    document.cookie = cookieName + " = " + cookieValue + "; expires = " +date.toGMTString();
},

//create any new cookie 
createCookieInHour('cookieName', 'cookieValue', 5);

//view cookies
let cookie = document.cookie.split(';');
console.log('cookie : ', cookie);
Comment

PREVIOUS NEXT
Code Example
Javascript :: getattribute javascript 
Javascript :: for loop javascript 
Javascript :: how to auto update package.json 
Javascript :: discord js send message to specific channel 
Javascript :: use theme in component mui 
Javascript :: console log 
Javascript :: set selected option jquery 
Javascript :: react buffer to image 
Javascript :: let var 
Javascript :: google map react search place 
Javascript :: cordova delete cache 
Javascript :: Best way to execute js only on specific page 
Javascript :: javascript createelement innerhtml 
Javascript :: push values to state array class react 
Javascript :: discord js v12 get user tag with id 
Javascript :: js maths 
Javascript :: sliding window algorithm javascript 
Javascript :: react sign in with linkedin 
Javascript :: How can I check if an object is an array 
Javascript :: javascript como recorrer un array multidimensional 
Javascript :: hti laravel route from javascript file 
Javascript :: useformik 
Javascript :: reactjs cdn file 
Javascript :: put new attribute on html tag using javascript 
Javascript :: convert milliseconds to time javascript 
Javascript :: lodash find duplicate element index 
Javascript :: js object entries 
Javascript :: javascript catch specific error 
Javascript :: pass data to slot vue 
Javascript :: js throw new error 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =