0

I haven't had a look at the source in-depth or anything yet, but was just curious if anyone has required or made use of this in the past or not.

It appears as though Redux is creating a singleton instance for the store and persisting this for the lifetime of the caller, but this is not really tenable for what we're thinking of implementing... what is the simplest approach, if one exists, for extending their API and creating stores with scoped lifetimes?

This is in the context of Redux being used in a purely server-side application (or at least as far as it can tell etc.)

Edit:

Similarly, while you can reference your store instance by importing it directly, this is not a recommended pattern in Redux. If you create a store instance and export it from a module, it will become a singleton. This means it will be harder to isolate a Redux app as a component of a larger app, if this is ever necessary, or to enable server rendering, because on the server you want to create separate store instances for every request.

I found this excerpt in their documentation here.

How does this module exporting consideration work in practice? If I only access the store within the scope I declared it for a given lifetime via externally exposed methods, should I expect the instance to be deallocated when the scope closes in some specified app lifetime?

Ole Pannier
  • 3,208
  • 9
  • 22
  • 33
Charles
  • 405
  • 4
  • 13
  • 1
    You should checkout the nextjs framework and especially `next-redux-wrapper`. It keeps the redux store a singleton on the client and dynamically makes stores for each request on the server. Pretty cool in my opinion. – Rishabh Anand Oct 31 '20 at 09:10
  • "*It appears as though Redux is creating a singleton instance for the store and persisting this for the lifetime of the caller*" - no, you're creating the store instance yourself [here](https://redux.js.org/tutorials/essentials/part-2-app-structure#creating-the-redux-store) and then provide it to all components [there](https://redux.js.org/tutorials/essentials/part-2-app-structure#providing-the-store). – Bergi Oct 31 '20 at 15:53
  • "*If I only access the store within the scope I declared it for a given lifetime via externally exposed methods*" - it's unclear what you mean by "externally exposed methods" here. Store are subject to garbage collection just like any other JavaScript object, references to them govern their lifetime. – Bergi Oct 31 '20 at 15:53

0 Answers0