0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

(メモ) mobx-state-treeでやりがちな循環依存

0
Posted at

(注意:糞記事です)

mobs-state-treeは便利なのですが、よく循環依存を作りがちです。

こういうのでコンパイルが死んでいませんか?

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

const Parent = types.model("Parent", {
    child: types.late(() => Child)
}).actions(self => {
    return {}
})

const Child = types.model("Child", {}).views(self => {
    return {
        get parent() {
            // 下記の評価結果は any になり、そしてエラーに!
            return getParentOfType(self, Parent)
        }
    }
})

でも下記はOKだったりします

const Parent = types.model("Parent", {}).views(self => {
    return {
        get child() {
            return Child.create({})
        }
    }
})

const Child = types.model("Child", {}).views(self => {
    return {
        get parent() {
            return getParentOfType(self, Parent)
        }
    }
})

これもOK

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

const Parent = types.model("Parent", {
    child: types.late(() => Child)
})

const Child = types.model("Child", {})
.actions(self => {
    const parent =  () => getParentOfType(self, Parent)
    return {
        doSomething() {
            parent().child
        }
    }
})
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?