mobx-state-tree.module.js?f7d3:2154 未捕获错误:[mobx-state-tree] 如果对象已经是同一或另一个状态树的一部分,则无法将对象添加到状态树。
我只是通过过滤另一个状态来得到这个错误:
const data = self.allData.slice(0, 20);
self.sliced = data
任何人都知道出了什么问题吗?
一个节点不能在状态树中存在两次。你可以改为建模你的sliced
数据作为数组references https://mobx-state-tree.js.org/concepts/references它应该按预期工作。
Example
import { types } from "mobx-state-tree";
const Thing = types.model({
id: types.identifier
});
const RootStore = types
.model({
allData: types.array(Thing),
sliced: types.maybe(types.array(types.reference(Thing)))
})
.actions((self) => ({
setSliced() {
const data = self.allData.slice(0, 20);
self.sliced = data;
}
}));
const rootStore = RootStore.create({
allData: Array.from({ length: 40 }, (_, index) => ({ id: index.toString() }))
});
console.log(rootStore.sliced); // undefined
rootStore.setSliced();
console.log(rootStore.sliced); // (20) [Object, Object, ...]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)