已存储字符串化对象但未显示

时间:2019-01-28 11:38:54

标签: reactjs

}, () => { 
      this.addTotals();
           const unified = Object.assign({}, {cart: 
           this.state.cart}, {subTotal: this.state.cartSubTotal})
           localStorage.setItem('myObject', 
           JSON.stringify(unified))
      });
};

在上面的代码中,我有一个回调函数(项目是一个小商店,用于购买东西供测试),在这里我对合并的对象进行字符串化,以便以后将其保存在本地存储中。 现在的问题是它不起作用,这意味着尽管当我使用开发人员工具时数据正在显示在本地存储中,但是当我刷新时,购物车组件中没有显示项目(产品和小计)。这是我的componentDidMount,我不确定错误是否在这里:

componentDidMount() {
    this.setProducts();
    this.setState({unified : !localStorage.getItem('myObject') 
           ? {}
           : JSON.parse(localStorage.getItem('myObject'))
    })

};

0 个答案:

没有答案