Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

New Flux store instance created upon second component referencing store #467

Open
bluebeag opened this issue Feb 14, 2019 · 7 comments
Open

Comments

@bluebeag
Copy link

I have one Flux store that is imported by two components. One a security based component and the other an app business data view. That Login.js component successfully logs in using Okta, obtains user info. from the application and stores it in the Flux store. The user navigates to the app data page, the dataview.js, which imports the store, uses it to retrieve user info before making fetch calls. The problem is the user state in the store is now undefined since the store has been newly constructed. It seems since the user info has not changed it should not be removed. The app data is still null since it hasn't retrieved the info yet. The store should behave like a singleton and ONLY update state when an action has been dispatched.

@kyldvs
Copy link
Contributor

kyldvs commented Feb 14, 2019

This is hard to debug without seeing the code to understand your issue, or a minimal repro. One thing to make sure you are doing is exporting an instantiated version of a store and not the store's class, like this in the examples: https://github.com/facebook/flux/blob/master/examples/flux-todomvc/src/data/TodoStore.js#L67

@bluebeag
Copy link
Author

Yes, that's what I'm doing. It's not behaving like a Singleton.

WTStore.js contain: export default new WTStore();

DataView.js (and Login.js) contain: import WTStore from '../stores/WTStore';

@kyldvs
Copy link
Contributor

kyldvs commented Feb 14, 2019

If you are doing that then I don't have a guess at what else might be wrong without seeing a more complete example. Can you repro the same issues in one of the examples? Or provide more code/minimal repro?

@bluebeag
Copy link
Author

I'll work on getting a simplified version that reproduces the behavior. Am I correct in that it shouldn't create a new instance of the store? Thank you.

@kyldvs
Copy link
Contributor

kyldvs commented Feb 14, 2019

I believe so, there should typically only be one instance of any given store. They should act like singletons.

@bluebeag
Copy link
Author

There is one instance at a given time. The problem is the store state is wiped when the second component references the store.

@kyldvs
Copy link
Contributor

kyldvs commented Feb 15, 2019

Without an example of the bug it's going to be hard for me to help you further. I've not heard of this happening before other than at times where someone is accidentally re-instantiating new instances of the store rather than it being used as a singleton.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants