-
-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
redux integration causes circular dependency #362
Comments
Is there a reason your store needs to be accessed by your screens? Generally you should use a |
I'm creating an action sheet handler in which I need to access state and dispatch actions that are not needed on the screen that initiates the action sheet. Furthermore, this action sheet can be initiated from several different screens. I've been trying to avoid pulling in state in each screen that is not required for the screens themselves simply to pass them into the action sheet handler if the action sheet handler can access state and dispatch actions directly itself. |
I ran into this exact issue. My components import my redux store because I personally prefer an explicit import to passing the store via context/Provider. This causes a circular dependency: Components <- Store <- Reducer <- Router <- Components. For me the most logical place to break this circle is between Router and Components. Conceptually it seems odd that the router's reducer, getStateForAction, ultimately depends upon view components (although I admit the resulting "screens" API is nice). The app view should depend on the app state, but not the other way around. Is there any way to create a purely static route config that doesn't involve view components (i.e. screens)? |
@smkhalsa If this helps, what I ended up doing for now was passing my Navigator.router to my reducer via a wrapper action that looks something like this:
My reducer is then able to handle it with something like this:
Not great, but it allowed me to keep the hacks localized to my routing code instead of changing my whole react/redux integration setup. I was also able to create that wrapper action in the dispatch method that I provide to "addNavigationHelpers", so as long as I use the helpers provided by that (props.navigation.navigate, etc.) I can forget about the whole mess. |
Another use-case: I need to access state in a static property, which can't be done using Unfortunately this is blocking me from migrating from Maybe the navigator could at least ignore/strip out static properties that are not |
I have also run into this problem. I ended up creating a singleton function to initialize (if needed) and return the store. It looks like this: import {createStore} from 'redux';
import rootReducer from './reducers';
let store;
export default function getStore() {
if(!store) {
store = createStore(rootReducer);
}
return store;
} Certainly not ideal, but a workaround. |
I created a storeRegistry and register the store while configuring it, and use this storeRegistry to access store outside react. App.js /* @flow */
import React from 'react';
import { Provider } from 'react-redux';
import AppNavigatorContainer from 'routes/AppNavigatorContainer';
import configureStore from 'store/configureStore';
import storeRegistry from 'store/storeRegistry';
const store = configureStore();
storeRegistry.register(store);
const App = () => (
<Provider store={store}>
<AppNavigatorContainer />
</Provider>
);
export default App; access the store outside react as below import storeRegistry from 'store/storeRegistry';
const store = storeRegistry.getStore(); |
Same issue. I need to access store when validating AuthToken. Store -> Service -> Actions -> Component |
I believe we're running into this now as well too. |
Couldn't come up with a workable solution to this. Sadly heading back to react-native-router-flux |
I think this exact problem is, what the I realized that my navigation actions do not need to import the Further, my js module, which wants to
I have the feeling, that this covers most usecases. |
The whole idea in this issue is not calling it from a component |
I can't comment on react-navigation specifically. I've still been bitten by a circular dependency chain created from importing my store to use somewhere outside of my component tree, similar to the examples above. That seems to be the focus here. I've researched this issue before and only came up with suggestions to use middleware. I may be missing something, but I fail to see how middleware is relevant to some of the problems I've encountered. I have to agree with @JulianKingman that accessing the current state at a given moment (not reactively) from within an arbitrary function (or event callback) can be very useful. I've personally settled on @gsaandy's solution of creating a reference to the store in an isolated module which doesn't actually import the store. His example is great. At first it felt dirty to reference the store that way, but it has worked out quite nicely outside of the component tree. There's no need to think about dependencies either, since |
@MilllerTime I use my store outside of components (in services) all the time and I don't have any circular dependency issues. I'd be interested in doing a screen share with you and looking at your code to see what exactly is happening. |
@dwilt I appreciate the offer! I think I got it though. After taking a hard look at my ad hoc usage of I suspect anyone else encountering circular refs when importing their store to access state in service/helper functions can resolve them by taking a look at their module structure and extracting functions which might live in the "wrong place". That's all I did for a couple offenders. However, I still need access to apiHelper.js <==> store.js Here, store.js imports the API helper and passes it to |
a PR to the documentation would be helpful for this! https://github.com/react-navigation/react-navigation.github.io closing as we are reserving this particular issue tracker for bugs |
Can you provide full example of Class please? |
@ifier see the below example from https://redux.js.org/recipes/configuring-your-store
|
@gsaandy can you share what exactly you have done in this files |
Hey! This issue is closed and isn't watched by the core team. You are welcome to discuss the issue with others in this thread, but if you think this issue is still valid and needs to be tracked, please open a new issue with a repro. |
|
I've implemented redux integration with the following reducer
where
Navigator
isI'm trying to import my redux
store
within my application. However, since the StackNavigator depends upon every component in my app, importing my reduxstore
creates a circular dependency.This results in
Navigator
being undefined in the above reducer.The text was updated successfully, but these errors were encountered: