[MST] Remove Model Instances from the Tree
2024-08-31 11:11:10
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)
}
}))
最新文章
- Delphi_04_Delphi_Object_Pascal_基本语法_02
- LintCode 392 House Robber
- spring源码学习之路---IOC实现原理(三)
- vimdiff
- 手机网站中 限制图片宽度 JS图片等比例缩放
- Objective-C Runtime 运行时之五:协议与分类
- Visual Studio 2015 Update 1 成功安装后运行 “出现未能正确加载[XXXX]包,此问题可能是由配置更改或安装另一个扩展导致的。” 可能的解决方法
- PHP 自学之路-----XML编程(Xpath技术,simpleXml技术)基础入门
- sdkman安装
- Collection<;E>;、Iterable<;T>;和Iterator<;E>;接口
- c#实现服务端webSocket
- PHP微信公众平台oauth2.0网页授权登录类的封装demo
- gitbook 入门教程之使用 gitbook.com 在线开发电子书
- python学习日记(练习,流程控制+数据结构)
- 20165232 预备作业3 Linux安装及学习
- day4函数文件操作
- 隐藏和显示服务器端控件以及Html控件
- IKAnalyzer 独立使用 配置扩展词典
- ssh连接超时自动断掉的解决办法
- 系统导出数据到excel,数据量过大(大约10W)条,导致服务器 cpu 100%解决方法
热门文章
- js滚动事件实现滚动触底加载
- 【Codeforces Round #476 (Div. 2) [Thanks, Telegram!] A】Paper Airplanes
- UVALive 2664 One-way traffic
- 洛谷 1063 dp 区间dp
- ASP.NET-POSTBACK是什么
- 洛谷 P2949 [USACO09OPEN]工作调度Work Scheduling
- 【IPC进程间通讯之三】内存映射文件Mapping File
- Codeforces 10A-Power Consumption Calculation(模拟)
- HDU 5273 Dylans loves sequence【 树状数组 】
- swift语言点评十三-Lazy