首页 > 解决方案 > 没有初始对象存储的再水合状态

问题描述

标题几乎解释了我的问题,我在我的组件中将对象存储在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)
  }

那么当存储中没有数据时,我该如何处理呢?

标签: reactjslocal-storage

解决方案


推荐阅读