Action used in another action #1873
-
How can I use action in another action? import { types, getSnapshot, applySnapshot, SnapshotIn } from "mobx-state-tree"
// @ts-ignore
export const MapModel = types
.model("Map",{
longitude: types.number,
latitude: types.number,
zoom: types.number
})
.views(self => ({
get viewState() {
return getSnapshot(self)
}
}))
.actions(self => ({
setViewState(snapshot: SnapshotIn<typeof MapModel>) {
applySnapshot(self, snapshot)
},
fitBounds(/** ... */) {
model.setViewState(/** ... */)
}
}))
const model = MapModel.create({
longitude: -122.41669,
latitude: 37.7853,
zoom: 13,
})
export default model
|
Beta Was this translation helpful? Give feedback.
Answered by
EmilTholin
Feb 7, 2022
Replies: 1 comment 2 replies
-
Hi @varna! This is discussed in length in the export const MapModel = types
.model("Map", {
longitude: types.number,
latitude: types.number,
zoom: types.number,
})
.views((self) => ({
get viewState() {
return getSnapshot(self);
},
}))
.actions((self) => ({
setViewState(snapshot: SnapshotIn<typeof MapModel>) {
applySnapshot(self, snapshot);
},
}))
.actions((self) => ({
fitBounds(/** ... */) {
self.setViewState(/** ... */);
},
})); |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
varna
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @varna!
This is discussed in length in the
TypeScript and MST
part of the documentation, and has a few solutions. I personally prefer splitting the.action
block up into two separate blocks: