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

react cookie

// server.jsrequire('@babel/register'); const express = require('express');const serverMiddleware = require('./src/server').default;const cookiesMiddleware = require('universal-cookie-express'); const app = express(); app  .use('/assets', express.static('dist'))  .use(cookiesMiddleware())  .use(serverMiddleware); app.listen(8080, function() {  console.log('Listening on 8080...');});
Comment

PREVIOUS NEXT
Code Example
Javascript :: is checked checkbox jquery 
Javascript :: js parse json 
Javascript :: .toJSON() in sequelize 
Javascript :: node javascript foreach limit 
Javascript :: round number 2 decimals javascript 
Javascript :: check user by id discord js 
Javascript :: javascript if string empty 
Javascript :: check if object has method javascript 
Javascript :: javascript and operator 
Javascript :: how to export a class in node js 
Javascript :: javascript splice without changing array 
Javascript :: change p text jqwuery 
Javascript :: draw on canvas from video element js 
Javascript :: force click btn using jquery 
Javascript :: flutter print json 
Javascript :: javascript if value is a string function 
Javascript :: js click counter 
Javascript :: chartjs each dataset get colors 
Javascript :: js count element tags 
Javascript :: how to enable click copy function using js 
Javascript :: on load page javascript 
Javascript :: how to show progress on ajax call 
Javascript :: domparser is not defined 
Javascript :: js array includes 
Javascript :: how to download file from link in react 
Javascript :: vue router refresh page not found 
Javascript :: post method 
Javascript :: header disallowed by preflight response in express 
Javascript :: find all voice chanels in category 
Javascript :: js console log multiple 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =