In this lesson we will dive a bit more into the tree semantics of MST.

In this lesson you will learn:

  • Actions can only modify their own subtree
  • The use of getParent to find the parent of a model instance
  • Using destroy to remove an instance entirely from the tree

The whole point for removing data from model is call 'destroy', sometime you might need 'getParent' if the data you want to remove is not in current tree node.

import { types, getParent, destroy } from "mobx-state-tree"

export const WishListItem = types
.model({
name: types.string,
price: types.number,
image: ""
})
.actions(self => ({
changeName(newName) {
self.name = newName
},
changePrice(newPrice) {
self.price = newPrice
},
changeImage(newImage) {
self.image = newImage
},
remove() {
getParent(self, 2).remove(self)
}
})) export const WishList = types
.model({
items: types.optional(types.array(WishListItem), [])
})
.actions(self => ({
add(item) {
self.items.push(item)
},
remove(item) {
destroy(item)
}
}))
.views(self => ({
get totalPrice() {
return self.items.reduce((sum, entry) => sum + entry.price, 0)
}
}))

最新文章

  1. Delphi_04_Delphi_Object_Pascal_基本语法_02
  2. LintCode 392 House Robber
  3. spring源码学习之路---IOC实现原理(三)
  4. vimdiff
  5. 手机网站中 限制图片宽度 JS图片等比例缩放
  6. Objective-C Runtime 运行时之五:协议与分类
  7. Visual Studio 2015 Update 1 成功安装后运行 “出现未能正确加载[XXXX]包,此问题可能是由配置更改或安装另一个扩展导致的。” 可能的解决方法
  8. PHP 自学之路-----XML编程(Xpath技术,simpleXml技术)基础入门
  9. sdkman安装
  10. Collection<E>、Iterable<T>和Iterator<E>接口
  11. c#实现服务端webSocket
  12. PHP微信公众平台oauth2.0网页授权登录类的封装demo
  13. gitbook 入门教程之使用 gitbook.com 在线开发电子书
  14. python学习日记(练习,流程控制+数据结构)
  15. 20165232 预备作业3 Linux安装及学习
  16. day4函数文件操作
  17. 隐藏和显示服务器端控件以及Html控件
  18. IKAnalyzer 独立使用 配置扩展词典
  19. ssh连接超时自动断掉的解决办法
  20. 系统导出数据到excel,数据量过大(大约10W)条,导致服务器 cpu 100%解决方法

热门文章

  1. js滚动事件实现滚动触底加载
  2. 【Codeforces Round #476 (Div. 2) [Thanks, Telegram!] A】Paper Airplanes
  3. UVALive 2664 One-way traffic
  4. 洛谷 1063 dp 区间dp
  5. ASP.NET-POSTBACK是什么
  6. 洛谷 P2949 [USACO09OPEN]工作调度Work Scheduling
  7. 【IPC进程间通讯之三】内存映射文件Mapping File
  8. Codeforces 10A-Power Consumption Calculation(模拟)
  9. HDU 5273 Dylans loves sequence【 树状数组 】
  10. swift语言点评十三-Lazy