我正在尝试从我从 json 加载的数据中设置 cookie 的值(将来自功能应用程序中的 axios 调用)。这是codeandbox的链接。如果你想看这里的代码是data.JSON:
{
"accounts": [
{
"accountid": "stuff",
"accountName": "moreStuff",
"anotherField": "someMoreStuff"
},
{
"accountid": "stuff",
"accountName": "moreStuff",
"anotherField": "someMoreStuff"
}]}
这是我的 index.js
const rootElement = document.getElementById("root");
ReactDOM.render(
<React.StrictMode>
<CookiesProvider>
<App />
</CookiesProvider>
</React.StrictMode>,
rootElement
);
这是我在 app.js 中使用 cookie 的方式
static propTypes = {
cookies: instanceOf(Cookies).isRequired
};
constructor(props) {
super(props);
const { cookies } = props;
this.state = {
AccountData: cookies.get("AccountData") || ["Option1","Option2","Option3","Option4"]
};
}
componentDidMount = () => {
const { cookies } = this.props;
console.log(AccountData);
let tempAccountData = AccountData;
cookies.set("AccountData", tempAccountData);
console.log(cookies.getAll());
};
getCookieValueLater = () => {
const { cookies } = this.props;
console.log(cookies.getAll());
};
render() {
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
<button onClick={() => this.getCookieValueLater()}>
getCookieValueLater
</button>
</div>
);
}
}
export default withCookies(App);
我认为插入带有字段的对象是一个问题。这是再次指向代码框的链接。感谢您的帮助,如果我在做一些愚蠢的事情,请随时告诉我。