When MST node is detached or destroyed? #1941
-
In official docs, MST is composed of "nodes" which represents Above explanation is what I understood through docs. Is it right? Then, when node is destoryed or detached from "tree"? I'm using this with React but I don't know the lifecycle(NOT LIFECYCLE HOOK) of node. To handle data properly, I need to know when node is destroyed or detached. Is node detached or destroyed when I call related method or another situation? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @baeharam! As the documentation states, the only thing that will detach the node is an explicit 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;
})
}));
Store.create();
// destroy! |
Beta Was this translation helpful? Give feedback.
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 adestroy(node)
call, or by removing or replacing the node from the tree.Example