我正在玩Mobx状态树。
在我建立的一个简单模型中,我收到此错误“ Uncaught TypeError:无法读取未定义的属性'name'”。
我正在粘贴所有代码块。
Index.js
import React,{Component} from "react";
import ReactDOM from 'react-dom';
import App from "./app";
import Invoice from "../models/Invoice"
const invoice = Invoice.create({currency:'EUR'}) ;
ReactDOM.render(<App invoice={invoice}/>,document.getElementById('root'));
app.Js
import React,{Component} from "react";
import {observer} from "mobx-react"
class App extends Component
{
render(){
const {invoice} = this.props;
return(
<div className="container-fluid">Invoicer
<h1>{invoice.status()} </h1>
{!invoice.isPaid && <button onClick={invoice.markPaid}>Pay</button>}
</div>
);
}
}
export default observer(App);
模型/ Invoice.js
import {types} from "mobx-state-tree";
import ItemList from "./ItemList";
const Invoice = types.model("Invoice",{
currency : types.string,
isPaid:false,
itemList : types.optional(ItemList,{items:[]})
})
.actions( self => ({
markPaid(){
self.isPaid = true;
}
}))
.views( self => ({
status(){
return self.isPaid ? "Paid,Go Home and Enjoy" :"Not Paid,work more and we will think about it";
}
}));
export default Invoice;
ItemList.js
import {types} from "mobx-state-tree";
import {Item} from "./Item";
const ItemList = types.model("ItemList",{
items: types.array(Item)
});
export default ItemList;
item.js
import {types} from "mobx-state-tree"
const Item = types.model("Item",{
quantity : types.number,
price: types.number,
comodityName: types.string
});
export default Item;
使用npm进行编译时,终端没有错误。在bundle.js中 错误在这一行
array: function(e) {
return new nt(e.name + "[]",e)
},
我不确定为什么会发生此错误?
答案 0 :(得分:0)
跳过ItemList并执行此操作
itemList : types.optional(types.array(Item, []))