react-navigation icon indicating copy to clipboard operation
react-navigation copied to clipboard

redux integration causes circular dependency

Open smkhalsa opened this issue 9 years ago • 21 comments

I've implemented redux integration with the following reducer

export function navigation(state, action) {
  return Navigator.router.getStateForAction(action, state)
}

where Navigator is

const Navigator = StackNavigator({
  Main: {
    screen: StacksInTabs,
  },
}, {
  mode: 'modal',
  headerMode: 'none',
})

I'm trying to import my redux store within my application. However, since the StackNavigator depends upon every component in my app, importing my redux store creates a circular dependency.

This results in Navigator being undefined in the above reducer.

smkhalsa avatar Feb 16 '17 17:02 smkhalsa

@gsaandy can you share what exactly you have done in this files image

configureStore.ts

import { applyMiddleware, createStore, Middleware } from 'redux';
import thunkMiddleware from 'redux-thunk';
import defaultLogger from 'redux-logger';
import { persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import rootReducer from './reducers/rootReducer';

const { composeWithDevTools } = __DEV__ ? { composeWithDevTools: v => v } : require('redux-devtools-extension');

const middlewares: Middleware[] = [thunkMiddleware];

if (__DEV__) {
  middlewares.push(defaultLogger);
}

const persistConfig = {
  key: 'root',
  storage,
  whitelist: ['session', 'appConfig'],
};
const persistedReducer = persistReducer(persistConfig, rootReducer);

const createStoreWithMiddleware = composeWithDevTools(applyMiddleware(...middlewares))(createStore);

export default (initialState = {}) => createStoreWithMiddleware(persistedReducer, initialState);

storeRegistry.ts

import configureStore from './configureStore';

class StoreRegistry {

  private storePersistor;
  private store = configureStore();

  getStore() {
    return this.store;
  }

  registerStorePersistor(persistor) {
    this.storePersistor = persistor;
  }

  getStorePersistor() {
    return this.storePersistor;
  }

}

export default new StoreRegistry();

gsaandy avatar Mar 08 '22 13:03 gsaandy