A pattern for sharing state between two stores #1933
-
I have: export const SensorStore = model({
selected: maybe(reference(Sensor)), And I needed to know if export const MenuStore = types
.model({ sensorSelected: false })
.actions((self) => ({
setSensorSelected(x: boolean) { self.sensorSelected = x; } |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
I'm not saying you should do this, but here's a pattern I came up with: First pass const sensorStore = useCreateStore(SensorStore)
const menuStore = useCreateStore(Menus, {}, { sensorStore }); Pick it up in interface ISensorStore extends Instance<typeof SensorStore> {}
interface IEnv { sensorStore: ISensorStore }
// menuStore
.actions((self) => ({
afterCreate() {
const env = getEnv<IEnv>(self); Then use reaction(
() => env.sensorStore.selected,
(selected) => self.setSensorSelected(selected !== undefined)
);
}, Thoughts? |
Beta Was this translation helpful? Give feedback.
-
Hi @davetapley! Another approach to this problem could be to go by the common root of your stores and return Example (CodeSandbox) const MenuStore = t.model("MenuStore").views((self) => ({
get sensorSelected() {
return getRoot<any>(self).sensorStore.selected !== undefined;
}
})); |
Beta Was this translation helpful? Give feedback.
-
Define dependency store as a volatile import authStore from 'stores/auth.store.js'
let HttpModel = types.model({})
.volatile(() => ({
authStore
}))
.views((self) => ({
get authorized() {
return self.authStore.authorized
}
})); Is this what you need? |
Beta Was this translation helpful? Give feedback.
Hi @davetapley!
Another approach to this problem could be to go by the common root of your stores and return
rootStore.sensorStore.selected !== undefined
as a view. This way you don't have to manually update this value.Example (CodeSandbox)