reactjs - 没有初始对象存储的再水合状态
问题描述
标题几乎解释了我的问题,我在我的组件中将对象存储在localforage
. 该对象稍后会很好地存储在组件中,但是如果我第一次进入组件/页面,则没有可从 localforage 检索的存储。
我正在补水,componentDidMount
但如果没有存储来更新状态值,则会引发错误并且组件崩溃。
async componentDidMount() {
localforage
.getItem("uriStore")
.then(localforageStore => {
// This code runs once the value has been loaded
// from the offline store.
console.log("localforage store in cDM ---> ", localforageStore);
this.setState({
Uris: localforageStore
});
})
.catch(error => console.log("Error -->", error)
}
那么当存储中没有数据时,我该如何处理呢?
解决方案
推荐阅读
- javascript - 过滤元素,如果没有结果显示这个
- sql-server - SSIS Foreach 循环容器建议
- xamarin - 如何使用 C# 将标签放置在 Grid 的前 66%
- shell - 如果用户的主目录中没有任何文件,则失败
- excel - 捕获用户错误并在错误后重新启用 application.events
- direct2d - Direct2D C++ 绘制椭圆下半部分的最简单方法
- c# - 功能完成后,等待 10 秒,然后重新运行相同的功能并取消
- python - 在 __init__ 中应用属性设置器逻辑
- apache-spark - 如何计算和获取 Spark Dataframe 中唯一 ID 的值总和?
- xamarin.forms - 如何为具有棱镜的 Xamarin 表单中的内容视图创建单独的视图模型?