【问题标题】:Redux state isn't being updated across the app in developmentRedux 状态未在开发中的应用程序中更新
【发布时间】:2020-05-16 19:19:22
【问题描述】:

我在使用 redux 时遇到了一个非常奇怪的问题。在我当前的应用程序中,我决定使用注销模式suggested by Dan Abramov

基本上是这样的:

const withLogout = (state, action) => {
  if (action.type === UserActions.logout.toString()) {
    state = undefined
  }
  return rootReducer(state, action)
}

分派注销操作后,状态将被清除。我向减速器添加了日志,它们都显示状态正在重置为默认值。 React Native Debugger 显示相同的内容。

但是,connect(react-redux) 函数内部的状态不会改变。更奇怪的是,该函数似乎检测到状态已更改的事实,因为 mapState 中的记录器函数正在被触发。但是记录器的输出显示那里的状态没有改变。组件也没有得到更新。

正如我在标题中提到的,此问题仅在开发模式下发生。一旦我构建应用程序并安装它,一切都开始按预期工作。而且不是Debugger造成的。

有什么想法吗?

【问题讨论】:

    标签: reactjs react-native redux react-redux redux-persist


    【解决方案1】:

    我找到了原因。我不得不在几个我无法使用react-redux 来调度操作的地方导入商店。这导致需要循环,有时会导致创建商店的多个实例。

    我偶然发现了以下解决方案,可确保您在整个应用程序中使用相同的商店实例:而不是在多个地方导入商店,而是使用以下模式:

        // somewhere in the code
        let store
        export const setStore = storeInstance => store = storeInstance 
        ...
        store.dispatch()
    
        // store index.js
        import { setStore } from 'someplace'
        ...
        setStore(store)
    

    【讨论】:

      猜你喜欢
      • 2018-06-29
      • 2020-04-11
      • 2017-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      相关资源
      最近更新 更多