[MST] Attach Behavior to mobx-state-tree Models Using Actions
Models are not just a nifty feature for type checking. They enable you to attach behavior to your actions in a straightforward and highly discoverable way
In this lesson, you will learn:
- How to define actions on models
- How to avoid
this
issues by using self - Models can only be modified using actions, and are further read-only from the outside
Action is the only way to update model, model is designed to be am immutable object. Even you use 'push', 'splice' in the actions, it won't affect.
import { types } 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
}
})) export const WishList = types
.model({
items: types.optional(types.array(WishListItem), [])
})
.actions(self => ({
add(item) {
self.items.push(item)
}
}))
Test:
import { getSnapshot, onSnapshot, onPatch } from "mobx-state-tree"
import { WishList, WishListItem } from "./WishList" it("can create a instance of a model", () => {
const item = WishListItem.create({
name: "Chronicles of Narnia Box Set - C.S. Lewis",
price: 28.73
}) expect(item.price).toBe(28.73)
expect(item.image).toBe("")
item.changeName("Narnia")
expect(item.name).toBe("Narnia")
}) it("can create a wishlist", () => {
const list = WishList.create({
items: [
{
name: "Chronicles of Narnia Box Set - C.S. Lewis",
price: 28.73
}
]
}) expect(list.items.length).toBe()
expect(list.items[].price).toBe(28.73)
}) it("can add new items", () => {
const list = WishList.create()
list.add(
WishListItem.create({
name: "Chesterton",
price:
})
) expect(list.items.length).toBe()
expect(list.items[].name).toBe("Chesterton")
list.items[].changeName("Book of G.K. Chesterton")
expect(list.items[].name).toBe("Book of G.K. Chesterton")
})
in the test, we do 'WishListIem.create':
it("can add new items", () => {
const list = WishList.create()
list.add(
WishListItem.create({
name: "Chesterton",
price:
})
) expect(list.items.length).toBe()
expect(list.items[].name).toBe("Chesterton")
list.items[].changeName("Book of G.K. Chesterton")
expect(list.items[].name).toBe("Book of G.K. Chesterton")
})
Since we already defined that each item should be a WishListItem in the model, therefore, we can skip 'WishListIem.create':
list.add({
name: "Chesterton",
price:
})
[MST] Attach Behavior to mobx-state-tree Models Using Actions的更多相关文章
- [MST] Create an Entry Form to Add Models to the State Tree
It is time to add new entries to the wishlist. We will achieve this by reusing forms and models we'v ...
- Vuex入门实践(中)-多module中的state、mutations、actions和getters
一.前言 上一篇文章<Vuex入门实践(上)>,我们一共实践了vuex的这些内容: 1.在state中定义共享属性,在组件中可使用[$store.state.属性名]访问共享属性 2.在m ...
- [MST] Remove Model Instances from the Tree
In this lesson we will dive a bit more into the tree semantics of MST. In this lesson you will learn ...
- [React + Functional Programming ADT] Connect State ADT Based Redux Actions to a React Application
With our Redux implementation lousy with State ADT based reducers, it is time to hook it all up to a ...
- 如何用 React 构建前端架构
早期的前端是由后端开发的,最开始的时候仅仅做展示,点一下链接跳转到另外一个页面去,渲染表单,再用Ajax的方式请求网络和后端交互,数据返回来还需要把数据渲染到DOM上.写这样的代码的确是很简单.在We ...
- react第三方库
作者:慕课网链接:https://www.zhihu.com/question/59073695/answer/1071631250来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- redux源码解析-函数式编程
提到redux,会想到函数式编程.什么是函数式编程?是一种很奇妙的函数式的编程方法.你会感觉函数式编程这么简单,但是用起来却很方便很神奇. 在<functional javascript> ...
- redux的源码解析
一. redux出现的动机 1. Javascript 需要管理比任何时候都要多的state2. state 在什么时候,由于什么原因,如何变化已然不受控制.3. 来自前端开发领域的新需求4. 我们总 ...
- Redux系列x:源码解析
写在前面 redux的源码很简洁,除了applyMiddleware比较绕难以理解外,大部分还是 这里假设读者对redux有一定了解,就不科普redux的概念和API啥的啦,这部分建议直接看官方文档. ...
随机推荐
- ES6之用let,const和用var来声明变量的区别
var(掌握) 不区分变量和常量 用var声明的变量都是变量,都是可变的,我们可以随便对它进行运算操作.这样当多个人进行同一个项目时,区分变量和常量会越来越难,一不小心就会把设计为常量的数据更改了 ...
- NOIP2018提高组金牌训练营——字符串专题
NOIP2018提高组金牌训练营——字符串专题 1154 回文串划分 有一个字符串S,求S最少可以被划分为多少个回文串. 例如:abbaabaa,有多种划分方式. a|bb|aabaa - 3 个 ...
- MyBatis学习总结(7)——Mybatis缓存
一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Se ...
- 利用VisualVM监视远程JVM
VisualVM介绍 VisualVM是集成了多个JDK命令工具的一个可视化工具,它主要用来监控JVM的运行情况,可以用它来查看和浏览Heap Dump.Thread Dump.内存对象实例情况.GC ...
- WinServer-IIS-SEO优化
来自为知笔记(Wiz)
- POJ 3270
黑书上的经典题了.我说说解这个题的巧妙的地方吧. 首先,竟然和置换联系起来了.因为其实一个交换即至少可以使其中一个元素到达指定位置了.和循环置换联合起来,使得一个循环内的数可以一步到达指定位置,很巧妙 ...
- openfiler作为文件server,实现ISCSI共享存储
还是不能发图.这是第二篇.图文在这个地址:http://download.csdn.net/detail/weimingyu945/8089893 1 登陆 首先登陆openfiler的we ...
- iOS开发- 生成/解析.vcf文件
vcf, 通讯录导出的一种格式. 一.生成vcf文件 假设要把我们iPhone通讯录里的数据, 生成vcf格式文件. 我们能够借助iCloud. 小技巧:通过iCloud导出iPhone通讯录的方法 ...
- iOS中respondsToSelector与conformsToProtocol的相关理解和使用
respondsToSelector相关的方法 : -(BOOL) isKindOfClass: classObj 用来判断是否是某个类或其子类的实例 -(BOOL) isMemberOfClass: ...
- 将枚举存入map集合
遍历枚举存入集合: Map<String,Object> deptLevel = new HashMap<>(); for(OrgBussinessEnum orgBussin ...