[英]cookie value is undefined (react-cookie 2.0.6)
I have a problem with implementing react cookies v^2.我在实现 react cookies v^2 时遇到问题。 I use webpack-dev-server for testing.我使用 webpack-dev-server 进行测试。
Here is a conlose log:这是一个结束日志:
Warning: Failed context type: The context cookies
is marked as required in withCookies(App)
, but its value is undefined
.警告:失败的上下文类型:上下文cookies
在withCookies(App)
标记为必需,但其值为undefined
。
in withCookies(App)在 withCookies(App)
in Provider在提供者中
/App.jsx: /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="/" component={LandingPage}/>
<Route path="/workspace" component={WorkSpace}/>
<Route exact path="/activation" component={ActivationPage}/>
<Route path="*" component={NotFoundPage}/>
</Switch>
</BrowserRouter>
</CookiesProvider>
);
}
}
export default withCookies(App);
/index.js: /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 './components/App';
const createStoreWithMiddleware = applyMiddleware(reduxThunk)(createStore);
const store = createStoreWithMiddleware(reducers);
ReactDOM.render(
<Provider store = {store}>
<App/>
</Provider>
,
document.getElementById('root'));
It appears that the functionality previously present in the react-cookie
npm package has been moved to universal-cookie
.以前在react-cookie
npm 包中存在的功能似乎已移至universal-cookie
。 The relevant example from the universal-cookie repository now is: 通用 cookie 存储库中的相关示例现在是:
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) 来源(所有学分均归功于原始答案的作者)
If you're convinced you're using universal-cookie
(or similar package) correctly, and still getting undefined
:如果您确信您正确使用了universal-cookie
(或类似的包),并且仍然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 HttpOnly
property).检查(在源代码中)如何设置 cookie (或使用 cookie 检查器浏览器扩展程序检查它,例如Chrome 的 EditThisCookie ,并检查HttpOnly
属性)。
If HttpOnly
is set to true
, then (by definition) no javascript will be able to access it (that's the point of HttpOnly
).如果HttpOnly
设置为true
,那么(根据定义)不需要JavaScript就可以访问它(这是该点的HttpOnly
)。 Hence the value would be returned as undefined
.因此,该值将作为undefined
返回。
From developer.mozilla.org/en-US/docs/Web/HTTP/Cookies :来自developer.mozilla.org/en-US/docs/Web/HTTP/Cookies :
Restrict access to cookies限制对 cookie 的访问
A cookie with the
HttpOnly
attribute is inaccessible to the JavaScriptDocument.cookie
API; JavaScriptDocument.cookie
API 无法访问具有HttpOnly
属性的 cookie; 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.例如,保留服务器端会话的 cookie 不需要对 JavaScript 可用,并且应该具有 HttpOnly 属性。 This precaution helps mitigate cross-site scripting ( XSS ) attacks.此预防措施有助于缓解跨站点脚本 ( XSS ) 攻击。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.