types.reference 没有解析 mobx-state-tree 中的引用

types.reference not resolving the reference in mobx-state-tree

我正在编写一个 returns 一个 ModelAModelB 数组的搜索函数,但是当我在我的组件中使用引用时,引用仍然是一个字符串。从文档中,我期望这是实际的 ModelAModelB 所以我觉得我做错了什么。

在我的根存储中,我创建了一个引用数组,如下所示:

searchResults: types.array(
      types.union(
        types.late(() => types.reference(ModelA)),
        types.late(() => types.reference(ModelB)),
      ),
),

当我从请求中取回它们时,它们被加载到商店中(我通过 store.modelBs.get(<one of the ids>) 检查,但组件仍然获取字符串数组:

有谁知道这是不是valid/what我这里做错了?

为了后代,我发现不可能有一个包含多个类型的引用数组。 Mobx-state-tree 无法确定 id 引用的模型类型并抛出错误:Failed to resolve reference 'idB1' to type 'AnonymousModel'.

CodeSandbox example

编辑:完成此操作的方法是使用 dispatcher config option for the union type like in this comment: https://github.com/mobxjs/mobx-state-tree/issues/1162#issuecomment-459742443