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.

Single dispatch for multiple stores with redux-middleware

See original GitHub issue

I have an app that currently use a Flux architecture with a single dispatcher and multiple stores that reacts to an action. I want to rewrite it with zustand but I’m not sure how to re-create this pattern. So basically I want multiple stores with reducer that react to an action, and one action is dispatched to every reducer in stores.

I created a simple exemple of my attempt here

The main thing here is

const dispatch = (...args) => {
  apiA.dispatch(...args);
  apiB.dispatch(...args);
}

I have a single dispatch function that will dispatch to every stores. This works great but is there a better way to handle it with zustand ? Or is zustand not made for this kind of architecture ?

Maybe my answer is the combine function, but I’m not sure how to implement it.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
simon-milletocommented, Mar 23, 2021

So, I found a way to achieve what I wanted, I’ve kept my reducer synchronous as described in the doc. And for the action where I need to run asynchronous, I do not use the returned value from reducer but I use the setState() function. I’m closing this issue, thanks for the help @dai-shi

0reactions
dai-shicommented, Mar 19, 2021

So, it’s like locking store updates. A naive way would be like this?

const useStore = create((set, get) => ({
  promise: null,
  data: {},
  setData: async (data) => {
    await get().promise
    set({ data })
  }
}))

const updateState = (fn) => {
  useStore.setState({
    promise: new Promise((resolve) => {
      fn()
      useStore.setState({ promise: null })
      resolve()
    })
  })
}

Not very confident if this works without pitfalls, though, depending on usage.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redux FAQ: Store Setup
Is it OK to have more than one middleware chain in my store enhancer? What is the difference between next and dispatch in...
Read more >
Where to dispatch multiple actions in redux? - Stack Overflow
If the second dispatch depends on the actions of first dispatch, you can get the state from the store, check if it satisfies...
Read more >
Create Redux Middleware to Dispatch Multiple Actions
We only have a few dispatching functions that need to be known by our React Application. Each one actually has multiple actions that...
Read more >
Javascript: Redux: Dispatching Multiple Actions - Medium
Probably one of the first things anyone starting out on Redux wonders is, can you dispatch multiple actions? The answer of course is,...
Read more >
Can I dispatch multiple actions from Redux action creators?
Actually, this is a trick question; with the appropriate store middleware, it is possible to do both at once! How does this work?...
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