_universalCookie.Cookies不是ReactJS中的构造函数错误

时间:2017-12-04 10:40:19

标签: reactjs cookies

如何在ReactJS中使用通用cookie?我收到此错误" _universalCookie.Cookies不是构造函数"。

import { Cookies } from 'universal-cookie';
...
     const cookies = new Cookies();
           cookies.set('myTokenCookies', 'response.data.token', { expires: 7, path: '/' });
           console.log(cookies.get('myTokenCookies'));

2 个答案:

答案 0 :(得分:1)

import * as Cookies from 'universal-cookie';

解决这个问题。我选择抛弃通用cookie并改为使用detect-browser软件包。

答案 1 :(得分:1)

您需要将Cookies导入为默认导出:

// option 1
const arrowFuncOne = () => {
  return {...someProps}
}

// option 2
const arrowFuncTwo = () => ({
   ...someProps
})

// This means that you returning an expression 
//same as

// return a result of other function
const arrowFuncCallFunOne = () => callSomeOtherFunc()
// or
const arrowCallFunkTwo = () => {return callSomeOtherFunc()}


// However
// wrong 
const arrowCallFunkNoReturn = () => {callSomeOtherFunc()}
// in this case the curly braces are just the body of the function  
// and inside this body there is no return at all

而不是

import Cookies from 'universal-cookie';