私はCodePenをReactJSプロジェクトに使用しています。ここcomponentDidUpdate()
ReactJSでlocalStorageを正しく設定する方法は?
componentDidUpdate(prevProps,prevState){
localStorage.setItem("myThings",JSON.stringify(this.state.Things));
}
:
this.state={
Things:{
element:"value",
element2: "value"
},
activated:-1
}
IのみThings
を設定する必要があるので、私は、コンポーネントのライフサイクルにlocalStorage
を設定することを決定した: Iである状態の一部をlocalStorage
を設定する必要がもし私がconsole.log(localStorage.getItem("myThings"))
と呼んだら私は記憶価値を得ていません。私は(私はあまりにも後者を試みたが)いずれかcomponentWillMount()
方法コンストラクタを好むべきで反応させドキュメントに読み取るので、私は、コンストラクタ中の溶液を実施localStorage
検査部について
。
constructor(props){
super(props);
if (localStorage.getItem("myThings")!==null){
this.state={
Things:JSON.parse(localStorage.getItem("myThings")),
activated:-1
}
}
...
typoがstackoverflowのみにありました –