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. [luogu] P3089 [USACO13NOV]POGO的牛Pogo-Cow

    P3089 [USACO13NOV]POGO的牛Pogo-Cow 题目描述 In an ill-conceived attempt to enhance the mobility of his pri ...

  2. snprintf

    snprintf(),函数原型为int snprintf(char *str, size_t size, const char *format, ...).   将可变参数 “…” 按照format的 ...

  3. 【 【henuacm2016级暑期训练】动态规划专题 K】 Really Big Numbers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 会发现如果x是reallynumber那么x+1也会是reallynumber.... (个位数+1,各位数的和+1了但是整个数也+ ...

  4. 在Windows系统下搭建Redis集群

    准备工作 需要4个部件:Redis.Ruby语言运行环境.Redis的Ruby驱动redis-xxxx.gem.创建Redis集群的工具redis-trib.rb.使用redis-trib.rb工具来 ...

  5. iis解析json

    一. windows XP 1. MIME设置:在IIS的站点属性的HTTP头设置里,选MIME 映射中点击”文件类型”-”新类型”,添加一个文件类型:关联扩展名:*.json内容类型(MIME):a ...

  6. echarts 设置x轴的和y轴的属性

    axisLabel:{ interval:0,//横轴信息全部显示 rotate:-30,//-30度角倾斜显示 } splitNumber:10

  7. HDU 2512

    水题 #include <iostream> #include <cstdio> #include <algorithm> #define LL __int64 # ...

  8. Codeforces Round #274 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/479 这次自己又仅仅能做出4道题来. A题:Expression 水题. 枚举六种情况求最大值就可以. 代码例如以下: #inc ...

  9. hbase的命令

    1.1. 命令 名称 命令表达式 创建表 create '表名', '列族名1','列族名2','列族名N' 查看所有表 list 描述表 describe  ‘表名’ 判断表存在 exists  ' ...

  10. 细数SuperComputer最新排名和常见Benchmark类型

    在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...