question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Discussion: Multiple stores in one component

See original GitHub issue

When using multiple stores in a component, it seems I have to change the style of which I consume a store. Store conventions can cause some headaches when using multiple stores in a component—conventions like: always having an actions key, or multiple stores may have a loaded key.

This shows the problem:

const Component = () => {
  // This is my preferred way of using zustand
  const { users, loaded, actions } = useUsersStore()
  // But now this code will break because of duplicate variable declarations
  const { comments, loaded, actions } = useCommentsStore()
}

So my only idea is to do this:

const Component = () => {
  const usersStore = useUsersStore()
  const commentsStore = useCommentsStore()

  // Then work with it as a domain object, which is kind of nice actually.
  if (usersStore.loaded) { ... }
  return <div>{ commentsStore.comments }</div>
}

However, things get a bit more complicate if I want to use selectors. Atomic selectors are promoted in the docs because they are showing simple use-cases, but those might be cumbersome here because I’d have to “namespace” each clashing variable, like so:

const Component = () => {
  const usersActions = useUsersStore(state => state.actions)
  const usersLoaded = useUsersStore(state => state.loaded)
  const comments = useCommentsStore(state => state.comments)
  const commentsActions = useCommentsStore(state => state.actions)
  const commentsLoaded = useCommentsStore(state => state.loaded)

  // Then work with it as a domain object, which is kind of nice actually.
  if (usersLoaded) { ... }
  return <div>{ comments }</div>
}

Maybe that’s not so bad. Thoughts?

I’d prefer the domain objects though with a nice selection API, maybe something that uses lodash pick and get under the hood.

const Component = () => {
  const usersStore = useUsersStore(['users', 'loaded', 'actions'])
  const commentsStore = useCommentsStore(['comments', 'loaded', 'actions'])

  // Then I just use the domain objects as shown before
}

The string shorthand would even be a very nice API for atomic selections…

const Component = () => {
  const usersActions = useUsersStore('actions')
  const usersLoaded = useUsersStore('loaded')
  const comments = useCommentsStore('comments')
  const commentsActions = useCommentsStore('actions')
  const commentsLoaded = useCommentsStore('loaded')
}

It could support lodash get style deep selections too…

const Component = () => {
  const loadAllUsers = useUsersStore('actions.loadAll')
}

If the core lib didn’t support this, would this be possible to add via a middleware?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
lucskycommented, Nov 30, 2019

@timkindberg Um, why don’t you simply rename your destructured variables ?

const Component = () => {
  const { users, loaded: usersLoaded, actions: usersActions } = useUsersStore()
  const { comments, loaded: commentsLoaded, actions: commentsActions } = useCommentsStore()
}
3reactions
rdhoxcommented, Feb 13, 2020

Just to add a thought to this issue, destructuring state like in the first post :

const Component = () => {
  // This is my preferred way of using zustand
  const { users, loaded, actions } = useUsersStore()
  // But now this code will break because of duplicate variable declarations
  const { comments, loaded, actions } = useCommentsStore()
}

or in the post just above mine make the use of zustand loose its interest, as the component will rerender when any state of the store is upload, since it’s subscribed to the all store, and not to a slice of it. That the React’s context behavior. Renaming the variables during the destructuring step is the natural solution, IMO.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple stores in one component · Issue #43 · pmndrs/zustand
When using multiple stores in a component, it seems I have to change the style of which I consume a store. Store conventions...
Read more >
Correct way of Creating multiple stores with mobx and ...
Question 1: What's the best way to organise stores and inject them into the component? Approach 1: App.js. // Root Store Declaration class ......
Read more >
Managing multiple store modules with Vuex - LogRocket Blog
What's the most painless way to manage multiple central stores and share data among nested components in Vue.js? Introducing Vuex.
Read more >
Have you ever used multiple stores & wrap your components ...
Have you ever used multiple stores & wrap your components using different stores with redux? : r/reactjs.
Read more >
Angular: NGRX clean architecture with multiple stores (Part 1)
In this article, I tried to deliver a technical guide on how to organize your application NGRX stores and provide you with examples...
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