Skip to content

When MST node is detached or destroyed? #1941

Answered by EmilTholin
baeharam asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @baeharam!

As the documentation states, the only thing that will detach the node is an explicit detach(node) call. However, a node will be destroyed by a destroy(node) call, or by removing or replacing the node from the tree.

Example

import { types, flow } from "mobx-state-tree";

const sleep = () => new Promise((resolve) => setTimeout(resolve, 100));

const Model = types.model("Model").actions((self) => ({
  beforeDestroy() {
    console.log("destroy!");
  }
}));

const Store = types
  .model("Store", {
    model: types.maybe(Model)
  })
  .actions((self) => ({
    afterCreate: flow(function* () {
      self.model = Model.create();

      yield sleep();

      self.model = undefined;

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by EmilTholin
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