How to solve the circular dependency problem in an nx workspace with react and and a redux store?
If for example I have a lib with all the store configuration there is a file createRootReducer - which combines my reducers:
import {
someSliceReducer,
SOME_SLICE_FEATURE_KEY,
} from '@app/feature';
export const createRootReducer = (history: History) =>
combineReducers({
router: connectRouter(history),
form: formReducer,
[SOME_SLICE_FEATURE_KEY]: someSliceReducer,
});
the application state in the store lib would be:
import { createRootReducer } from './createRootReducer';
export type ApplicationState = ReturnType<ReturnType<typeof createRootReducer>>;
The problem comes from importing the application state in the lib where the someSliceReducer lives:
import {ApplicationState} from '@app/store'
const entities= useSelector(
(state: ApplicationState) => state.dischargeFolder.loadingStatus
);
now we have a circular dependency and nx warns us about it with:
Circular dependency between "feature" and "store" detected: feature -> store -> feature eslint@nrwl/nx/enforce-module-boundaries
How could this be prevented? I need to import the reducer in the root-reducer, but i also need to import the ApplicationState into the feature. I tried to extract the root-reducer into an own lib - but this doesnt sove the problem nx complains about a circular dependendcy over 3 libs.
I also tried to have "reducerRegistry" which takes an reducer and registers it internally - unfortunatly with this approach I lose the type information of the registered reducer and the ApplicationState is not correctly infered.