Rematch: [Feature] Why not `const { store, dispatch } = init({ models });`?

Created on 9 Mar 2018  路  6Comments  路  Source: rematch/rematch

use scoped dispatch rather than global dispatch, because rematch also used scoped store.

mirrorx:

  1. use global store import * as store_module from 'mirrorx/lib/store'; const getStore = () => store_module.store;;
  2. use global actions;

rematch:

  1. use scoped store const store = init({ models });;
  2. But why use global dispatch, it should use scoped dispatch;
question

Most helpful comment

You're right. We are currently working on making Rematch work with multiple stores. In order to do this, we have to implement some of the changes you've described.

Expect a few changes:

  • you can, and should use scoped dispatch. It's accessible as store.dispatch, and usable within connect.
  • in the future, global dispatch will be available, and will fire into all stores. Scoped dispatch is recommended.
  • global getState is currently deprecated and will be removed

I hope you'll agree, when we are done, Rematch will have a more scoped and modular design.

If you have other suggestions, please let us know :)

All 6 comments

You're right. We are currently working on making Rematch work with multiple stores. In order to do this, we have to implement some of the changes you've described.

Expect a few changes:

  • you can, and should use scoped dispatch. It's accessible as store.dispatch, and usable within connect.
  • in the future, global dispatch will be available, and will fire into all stores. Scoped dispatch is recommended.
  • global getState is currently deprecated and will be removed

I hope you'll agree, when we are done, Rematch will have a more scoped and modular design.

If you have other suggestions, please let us know :)

Sorry, I just reread your post from the title.

const { store, dispatch } = init({ models })

There are a few reasons this is less than ideal.

The Problem

Dispatch needs to be exported to other files.

export const { store, dispatch } = init({ models })

However, import/exports are run at startup and will result in an imported dispatch that is undefined.

Hacky Solutions

There is a way around this.

export let fileDispatch

const { store, dispatch } = init({ models })
fileDispatch = dispatch

But I don't think anyone wants to see that.

There's another way to do this with classes, but also not pretty.

Well, store.dispatch as scoped dispatch is OK.

But export const { store, dispatch } = init({ models }) won't lead to an undefined dispatch.

Yeah, you're right. As long as dispatch is called before the rest of the app, and all imports are loaded later, this should work. I agree, this IS a good idea.

Though after some thought, I think a simpler solution can be reached to access scoped dispatch.

export const store = init()
export const { dispatch } = store

agree

Thanks again. You've outlined a problem: we should move people towards using scoped dispatch.

Globals are not a good idea.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

miraage picture miraage  路  4Comments

alexicum picture alexicum  路  5Comments

huyansheng3 picture huyansheng3  路  3Comments

canyavall picture canyavall  路  6Comments

ShMcK picture ShMcK  路  4Comments