4

我想知道我想要实现的目标是否可行,如果可以,那么我想找出我做错了什么。我正在使用 react-native-threads 库来创建一个单独的“线程”。里面应该有一些 setInterval 函数来检查应用程序的当前状态,比如每 20 秒。现在在那个线程中,我试图做一些类似于这个问题的最佳答案中描述的事情: 在反应组件之外访问 redux 存储的最佳方法是什么? 但是,我使用的是 redux-persist,所以我在存储配置文件中没有一个普通的“存储”变量。店铺设置代码:

    const rootReducer = combineReducers({
    appState: appStateReducer,
    entries: entriesReducer,
    listHelper: listHelperReducer,
    authenticate: authenticateReducer
})

const persistConfig = {
    key: 'root',
    storage,
    stateReconciler: autoMergeLevel2,
    blacklist: [ 'appState' , 'listHelper'],
    whitelist: ['authenticate' , 'entries']

}
const pr = persistReducer(persistConfig, rootReducer)
const configureStore = () => {
    const store = createStore(pr,  devToolsEnhancer({ realtime: true, hostname: 'localhost', port: 8082 }))

    const persistor = persistStore(store)
    return { persistor, store }
}
export default configureStore

我尝试从该文件导入 configureStore 并在 index.thread.js 中访问这样的状态:

console.tron.log(configureStore().store.getState());

但是,这是返回初始状态。我需要补液后的当前状态。有没有人试图解决类似的问题?我将非常感谢任何提示。谢谢你。

编辑:这是调用 configureStore 的 index.js 文件。

const store = configureStore();
const RNRedux = () => (
  <Provider store={store.store}>
    <PersistGate loading={<LoadingScreen />} persistor={store.persistor}>
      <App />
    </PersistGate>
  </Provider>
);


AppRegistry.registerComponent("ProjectName", () => RNRedux);
4

2 回答 2

1

您可以在商店文件中调用 createStore 并从那里导出。

const pr = persistReducer(persistConfig, rootReducer);
const store = createStore(pr,  devToolsEnhancer({ realtime: true, hostname: 'localhost', port: 8082 }))
const configureStore = () => {
    const persistor = persistStore(store)
    return { persistor, store }
}

export default configureStore

现在您可以使用如下所示的商店。

console.tron.log(configureStore.store.getState());
于 2018-09-17T15:12:26.137 回答
0

我发现我的问题是文件之间的循环依赖关系。这留下了一些未初始化的变量,所以我建议仔细检查。

于 2019-09-25T14:08:27.933 回答