我的MobX状态树模型是这样的
const ProductItem = types
.model({
name: types.string,
price: types.number
})
.actions(self => ({
changePrice(newPrice) {
self.price = newPrice;
}
}));
const ProductStore = types
.model({
items: types.optional(types.array(ProductItem), [])
})
.actions(self => ({
add(item) {
self.items.push(item);
}
}));
const AppStore = types.model('AppStore', {
productStore: types.maybeNull(ProductStore)
});AppStore是根存储区。
我想为ProductStore创建AppStore并初始化下面的数据。我已经创建了以下函数来初始化和创建存储:
export const initializeStore = (isServer, snapshot = null) => {
if (isServer) {
AppStore.create({
.....
});
}
return store;
};我不确定应该如何在AppStore.create()中用这个数组初始化ProductStore:
items: [
{
name: 'Product 1',
price: 150
},
{
name: 'Product 2',
price: 170
}
]任何帮助都将不胜感激。
发布于 2020-06-20 05:21:33
初始数据可以这样给出
AppStore.create({
productStore: {
items: [
{
name: 'Product 1',
price: 150
},
{
name: 'Product 2',
price: 170
}
]
}
});因为ProductStore在您的AppStore中的productStore key下使用。
https://stackoverflow.com/questions/59764699
复制相似问题