bcherny / undux

⚡️ Dead simple state for React. Now with Hooks support.

Home Page:https://undux.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Accessing the store from outside react

mitsuhiko opened this issue · comments

How can I get the underlying store from createConnectedStore now that createStore is deprecated? More importantly how do I use this without react now?

Hey @mitsuhiko! You can still use createStore for now - if there's interest in keeping it around, we may just rename it to createSingletonStore in the future.

For React users, the big benefit of createConnectedStore is it means stores can now get garbage collected when the view unmounts. The way it works is it doesn't actually create a store (a bit of a misnomer), but it gives you the two pieces you need to let React construct a store instance for you. For non-React users, I'd love to see similar bindings for your framework of choice.

Which framework are you using? Or, just raw JS?

@bcherny I am actually using it with react now but I have some utility code that is itself not using react. For instance my API layer accesses auth tokens from the store and wants to notify the store if the requests fail because the user session was invalidated. That API layer is largely disconnected from my react code.

@mitsuhiko In that case, I'd stick to using createStore for now, or move your API code closer to your React code. If you can share your code (or repro case), I'm happy to give more specific suggestions.

commented

I'm also facing similiar problem. I was trying to build extensions/plugins for my react app and decided to expose the undux store for non-react plugins/extensions to mutate. It works great now but I'm not sure about the performance when there are multiple plugins trying to update the same store.

@giddens9527 Can you talk a little more about your use case, and what your specific performance concern is? A code sample would be helpful.

I have faced the same issue. I created some services to share commonly used code across many components, but in one case I need some values that are present inside the store. The only way without using createStore is by passing the store prop as an argument to the service function, which is cumbersome. Can' createConnectedStore also output the store itself, the same way as the prop is passed to components? If not, I am fairly certain that the createStore, as only solution, would be valuable to keep around and un-deprecate it.

By the way, when using createStore and connect, how do I get the Container used to wrap the application?

Can' createConnectedStore also output the store itself, the same way as the prop is passed to components?

No. createConnectedStore is different than createStore, because it lets React manage and instantiate (via Context) the store for you. This has two big benefits:

  1. It avoids top-level references to store instances, meaning they can get properly garbage collected when they're no longer used.
  2. It lets you create multiple instances of a store.

By the way, when using createStore and connect, how do I get the Container used to wrap the application?

createStore doesn't need a Container, since it creates a singleton store.

So I can simply use the withStore HOC on my components?