[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
thisissues 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啥的啦,这部分建议直接看官方文档. ...
随机推荐
- java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap
最近在使用java PiO导入导出Excle在windos本机上运行没有问题: 但是!!问题来了!放到Linux服务器上部署后出现异常 java.lang.NoClassDefFoundError: ...
- 【codeforces 810A】Straight «A»
[题目链接]:http://codeforces.com/contest/810/problem/A [题意] 有n门课的成绩,和一个整数k代表每门课的满分都是k分; 然后这n门课的成绩是按照平均分算 ...
- ZJU 2605 Under Control
Under Control Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ...
- jQuery判断浏览器类型和版本
jquery 判断浏览器类型 例: if($.browser.msie) { alert("这是一个IE浏览器"); }else if($.browser.opera) { a ...
- POJ 2369
我们知道,当循环长度为L时,置换群幂次为K ,则结果是GCD(L,K)个积相乘. 于是,我们只需要求出每个循环的长度,求得它们的最小公倍数即为解. #include <iostream> ...
- HDU 2512
水题 #include <iostream> #include <cstdio> #include <algorithm> #define LL __int64 # ...
- [Javascirpt] Developer-friendly Flow Charts with flowchart.js
Flowchart.js is a great tool for creating quick, simple flowcharts in a way that keeps you out of a ...
- UnrealEngine4初始化流程
自古以来全部的游戏引擎都分为三个大阶段:Init,Loop,Exit.UE4也不例外. 首先找到带有入口函数的文件:Runtime/Launch/Private/XXXX/LaunchXXXX.cpp ...
- hdoj--1384--Intervals(差分约束)
Intervals Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- 使用Swing组件编写一个支持中文文本编辑程序ChineseTextEdit.java
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class C ...