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的更多相关文章

  1. [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 ...

  2. Vuex入门实践(中)-多module中的state、mutations、actions和getters

    一.前言 上一篇文章<Vuex入门实践(上)>,我们一共实践了vuex的这些内容: 1.在state中定义共享属性,在组件中可使用[$store.state.属性名]访问共享属性 2.在m ...

  3. [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 ...

  4. [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 ...

  5. 如何用 React 构建前端架构

    早期的前端是由后端开发的,最开始的时候仅仅做展示,点一下链接跳转到另外一个页面去,渲染表单,再用Ajax的方式请求网络和后端交互,数据返回来还需要把数据渲染到DOM上.写这样的代码的确是很简单.在We ...

  6. react第三方库

    作者:慕课网链接:https://www.zhihu.com/question/59073695/answer/1071631250来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...

  7. redux源码解析-函数式编程

    提到redux,会想到函数式编程.什么是函数式编程?是一种很奇妙的函数式的编程方法.你会感觉函数式编程这么简单,但是用起来却很方便很神奇. 在<functional javascript> ...

  8. redux的源码解析

    一. redux出现的动机 1. Javascript 需要管理比任何时候都要多的state2. state 在什么时候,由于什么原因,如何变化已然不受控制.3. 来自前端开发领域的新需求4. 我们总 ...

  9. Redux系列x:源码解析

    写在前面 redux的源码很简洁,除了applyMiddleware比较绕难以理解外,大部分还是 这里假设读者对redux有一定了解,就不科普redux的概念和API啥的啦,这部分建议直接看官方文档. ...

随机推荐

  1. 线性回归(regression)

    简介 回归分析只涉及到两个变量的,称一元回归分析.一元回归的主要任务是从两个相关变量中的一个变量去估计另一个变量,被估计的变量,称因变量,可设为Y:估计出的变量,称自变量,设为X. 回归分析就是要找出 ...

  2. python hashlib、configparse、logging

    一.hashlib 1.Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等.     2.摘要算法 通过摘要函数f()对任意长度的数据data计算出固定长度的摘要digest,目 ...

  3. 小学生都能学会的python(函数)

    小学生都能学会的python(函数) 神马是函数 函数: 对功能或者动作的封装 函数的定义 def 函数名(形参列表): 函数体(return) ret = 函数名(实参列表) 函数的返回值 retu ...

  4. MyBatis学习总结(18)——MyBatis与Hibernate区别

    也用了这么久的Hibernate和MyBatis了,一直打算做一个总结,就他们之间的优缺点说说我自己的理解: 首先,Hibernate是一个ORM的持久层框架,它使用对象和我们的数据库建立关系,在Hi ...

  5. Android Material Design-Getting Started(入门)-(一)

    转载请注明出处:http://blog.csdn.net/bbld_/article/details/40400343 翻译自:http://developer.android.com/trainin ...

  6. apiCloud中openFrameGroup传参

    apiCloud中openFrameGroup传参 1.无效的 api.openFrameGroup({ // 打开 frame 组 name: 'group', scrollEnabled: fal ...

  7. webpack的像素转vw loader插件

    这是一款针对webpack的像素转vw单位的loader插件. 笔者公司中,h5 rem的开发方案目前已经渐渐开始转向vw方案,因此本工具应运而生. 目前所制作的h5,大部分设计稿分辨率都是750的宽 ...

  8. ubuntu18.04中安装iNode

    title: ubuntu18.04中安装iNode toc: false date: 2018-09-01 17:52:20 categories: methods tags: ubuntu iNo ...

  9. Kettle的四大不同环境工具

    不多说,直接上干货! kettle里有不同工具,分别用于ETL的不同阶段. 初学者,建议送Spoon开始.高手,是四大工具都会用. Sqoop: 图形界面工具,快速设计和维护复杂的ETL工作流.集成开 ...

  10. Wireshark filter语法

    过滤器语法 ------------------------------------------------------------- 最简单的过滤允许你检查一个协议或者字段的存在.如果你想查看所有的 ...