最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - cookie value is undefined (react-cookie 2.0.6) - Stack Overflow

programmeradmin3浏览0评论

I have a problem with implementing react cookies v^2. I use webpack-dev-server for testing.
Here is a conlose log:

Warning: Failed context type: The context cookies is marked as required in withCookies(App), but its value is undefined.
in withCookies(App)
in Provider

/App.jsx:

import React, { Component } from 'react';
import { CookiesProvider, withCookies, Cookies} from 'react-cookie'
import {Route, Switch, BrowserRouter} from 'react-router-dom';

//import RequireAuth from './RequireAuth';
import NotFoundPage from './NotFoundPage';
import LandingPage from './LandindPage';
import WorkSpace from './WorkSpace';
import ActivationPage from './ActivationPage';

class App extends Component {
  render() {
    return (
      <CookiesProvider>
        <BrowserRouter>
          <Switch>
            <Route exact={true} path="/" ponent={LandingPage}/>
            <Route path="/workspace" ponent={WorkSpace}/>
            <Route exact path="/activation" ponent={ActivationPage}/>
            <Route path="*" ponent={NotFoundPage}/>
          </Switch>
        </BrowserRouter>
      </CookiesProvider>
    );
  }
}

export default withCookies(App);

/index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import { createStore, applyMiddleware } from 'redux';
import reduxThunk from 'redux-thunk';
import reducers from './reducers';
import { Provider } from 'react-redux';

import App from './ponents/App';

const createStoreWithMiddleware = applyMiddleware(reduxThunk)(createStore);
const store = createStoreWithMiddleware(reducers);

ReactDOM.render(
  <Provider store = {store}>
    <App/>
  </Provider>
  ,
  document.getElementById('root'));

I have a problem with implementing react cookies v^2. I use webpack-dev-server for testing.
Here is a conlose log:

Warning: Failed context type: The context cookies is marked as required in withCookies(App), but its value is undefined.
in withCookies(App)
in Provider

/App.jsx:

import React, { Component } from 'react';
import { CookiesProvider, withCookies, Cookies} from 'react-cookie'
import {Route, Switch, BrowserRouter} from 'react-router-dom';

//import RequireAuth from './RequireAuth';
import NotFoundPage from './NotFoundPage';
import LandingPage from './LandindPage';
import WorkSpace from './WorkSpace';
import ActivationPage from './ActivationPage';

class App extends Component {
  render() {
    return (
      <CookiesProvider>
        <BrowserRouter>
          <Switch>
            <Route exact={true} path="/" ponent={LandingPage}/>
            <Route path="/workspace" ponent={WorkSpace}/>
            <Route exact path="/activation" ponent={ActivationPage}/>
            <Route path="*" ponent={NotFoundPage}/>
          </Switch>
        </BrowserRouter>
      </CookiesProvider>
    );
  }
}

export default withCookies(App);

/index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import { createStore, applyMiddleware } from 'redux';
import reduxThunk from 'redux-thunk';
import reducers from './reducers';
import { Provider } from 'react-redux';

import App from './ponents/App';

const createStoreWithMiddleware = applyMiddleware(reduxThunk)(createStore);
const store = createStoreWithMiddleware(reducers);

ReactDOM.render(
  <Provider store = {store}>
    <App/>
  </Provider>
  ,
  document.getElementById('root'));
Share Improve this question edited May 10, 2017 at 17:32 svnvav asked May 10, 2017 at 12:17 svnvavsvnvav 3371 gold badge8 silver badges16 bronze badges 1
  • So, to solve the problem I've changed the react-cookie lib to universal-cookie and it works. But the question remains, why it's not defined? Should it be defined on the server side? (but there is an example in the README on react-cookie github without server) – svnvav Commented May 10, 2017 at 18:07
Add a ment  | 

2 Answers 2

Reset to default 8

It appears that the functionality previously present in the react-cookie npm package has been moved to universal-cookie. The relevant example from the universal-cookie repository now is:

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

Source (all credits to author of the orginal answer)

HttpOnly

If you're convinced you're using universal-cookie (or similar package) correctly, and still getting undefined:
Check (in the source code) how the cookie is being set (or inspect it with a cookie inspector browser extension, like EditThisCookie for Chrome, and check the HttpOnlyproperty).
If HttpOnly is set to true, then (by definition) no javascript will be able to access it (that's the point of HttpOnly). Hence the value would be returned as undefined.

From developer.mozilla/en-US/docs/Web/HTTP/Cookies:

Restrict access to cookies

A cookie with the HttpOnly attribute is inaccessible to the JavaScript Document.cookie API; it is sent only to the server. For example, cookies that persist server-side sessions don't need to be available to JavaScript, and should have the HttpOnly attribute. This precaution helps mitigate cross-site scripting (XSS) attacks.

发布评论

评论列表(0)

  1. 暂无评论