在redux存储中刷新状态树

时间:2018-03-08 07:59:16

标签: javascript reactjs redux local-storage

我是react-redux的新手,在数据流中遇到困难。我的redux结构中有多个reducers,它们通过" combineReducers "组合。

现在存储在localStorage中填充了值。它已初始化,所有操作也通过" dispatch()"执行。但这里是一个场景需要从locastorage刷新整个商店树,因为localstorage已更新,但商店尚未更新,直到刷新页面。

尝试的解决方案如下:

  1. 声明一个动作,当更新localstorage时会调度该动作。它也会在主存储文件中被监听,如下所示。

     let currentValue
    function handleChange() {
      let previousValue = currentValue
      currentValue = JSON.parse(window.localStorage.getItem('data')) 
      if (previousValue !== currentValue) {
        console.log(
          'Some deep nested property changed from',
          previousValue,
          'to',
          currentValue
        )
      }
    }
    
    store.subscribe(handleChange);
    
  2. 控制台说商店在这里更新了,但是状态并没有在这里更新显示更新的组件。

    任何建议请问我做错了什么。

0 个答案:

没有答案