Export the store from the module you called createStore
with. Then you are assured it will both be created and will not pollute the global window space.
MyStore.js
const store = createStore(myReducer);
export store;
or
const store = createStore(myReducer);
export default store;
MyClient.js
import {store} from './MyStore'
store.dispatch(...)
or if you used default
import store from './MyStore'
store.dispatch(...)
For Multiple Store Use Cases
If you need multiple instances of a store, export a factory function.
I would recommend making it async
(returning a promise
).
async function getUserStore (userId) {
// check if user store exists and return or create it.
}
export getUserStore
On the client (in an async
block)
import {getUserStore} from './store'
const joeStore = await getUserStore('joe')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…