Accessing store from another store

See original GitHub issue

Is it possible to access a store from another store?

Or is this an anti-pattern?

Is store composition the way to go?

Or do you just access the raw state like such:

// Getting non-reactive fresh state

const paws = useBearStore.getState().paws

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, Jul 12, 2021

I had been wondering it too, but just recently got the idea to relate with flux/redux.

We should model the store like this:

const useStore = create((set) => ({
  storeSliceA: ...,
  storeSliceB: ...,
  storeSliceC: ...,
  dispatchX: () => set(...),
  dispatchY: () => set(...),
}))

say you have state related to your network client, your user, products, orders, etc

those are store slices.

This is an idiomatic way in zustand, but zustand itself is unopinionated and using multiple stores is totally fine.

1reaction
dai-shicommented, Jul 12, 2021

The primary use case is a single store. Multiple stores make sense if they are totally isolated. It sounds like for your use case, a single store would be a good pattern.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Accessing store from another store · Issue #44 · pmndrs/zustand
I'm making this use-case up, so it might be unrealistic. Or would it be better to force components to compose stores together like...
Read more >
Accessing the Store | React Redux
Accessing the Store. React Redux provides APIs that allow your components to dispatch actions and subscribe to data updates from the store.
Read more >
Access Stores from Other Stores - A Vue.js Lesson From our ...
In this lesson, we learn how to access a Pinia store from another Pinia store. Thankfully, with Pinia the process is quite intuitive...
Read more >
Access the Redux Store Outside a React Component
If you need to dispatch actions from outside a React component, the same technique will work: import the store, then call store. dispatch()...
Read more >
How to access store state from a different store module in vuex?
Actions take a context object as parameter, you can use it to access rootState. Check documentation for other available options.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found