【发布时间】:2017-11-03 10:51:03
【问题描述】:
首先我检查是否支持Localstorage,如果支持,我检查localstorage中是否有项目查询。如果有一个项目,它应该更新到查询状态。在 setState 之前,我有一个警报,它会很好地提醒该项目。但是在 setState 之后我再次发出警报但它什么也没显示?有人知道为什么吗?即使我将this.setState({query: localStorage.getItem("query")}) 替换为this.setState({query: 'test'}),它也不会在警报中显示测试?
getLocalStorage = () => {
if (typeof(Storage) !== "undefined") {
if (localStorage.getItem("query") !== null) {
//Alerts 'a string'
alert(localStorage.getItem("query"));
this.setState({
query: localStorage.getItem("query")
});
//Alerts nothing?
alert(this.state.query);
this.search();
}
else{
this.getLocation();
}
}
else {
this.getLocation();
}
};
【问题讨论】:
标签: javascript reactjs