Skip to content

Action used in another action #1873

Answered by EmilTholin
varna asked this question in Q&A
Feb 7, 2022 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

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:

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(/** ... */);
    },
  }));

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@varna
Comment options

@EmilTholin
Comment options

Answer selected by varna
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants