[MST] Describe Your Application Domain Using mobx-state-tree(MST) Models
2024-08-31 13:47:21
In this lesson, we introduce the running example of this course, a wishlist app. We will take a look at the core of mobx-state-tree (MST), models. Models describe the shape of your state and perform type validation.
You will learn:
- Defining models using types.Model
- Instantiating models from JSON using Model.create
- Primitive types: types.string & types.number
- Type inference for primitive types
- types.array
- types.optional
- Composing models into a model tree
- Testing models using jest
To create a model:
import { types } from "mobx-state-tree" export const WishListItem = types.model({
name: types.string,
price: types.number,
image: ""
}) export const WishList = types.model({
items: types.optional(types.array(WishListItem), [])
})
'types' is similar to React PropTypes.
Once model is created, then we can write tests to verify:
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("")
}) 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)
})
最新文章
- 安装SQL SERVER 2005出现“sql2005 执行安装向导期间出错 ”
- jsp日期插件My97DatePicker
- linux下如何查找需要的文件后并删除
- X86 架构和 ARM 架构
- css基础学习
- C#模拟POST提交表单(二)--HttpWebRequest以及HttpWebResponse
- BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊(动态树)
- 在没有DOM操作的日子里,我是怎么熬过来的(中)
- monkey事件简介
- windows配置Erlang环境
- springBoot项目启动类启动无法访问
- geeksforgeeks-Array-Rotation and deletion
- 图->;定义
- JavaScript Transpilers: 为什么需要用它们?Babel的使用介绍。
- docker网络之overlay
- windows10中远程登录ubuntu16.04的桌面
- 关于Android中传递数据的一些讨论
- android camera preview常用格式
- pythong 中的 __call__
- HDU 1317 XYZZY(floyd+bellman_ford判环)
热门文章
- C#WIFI搜索与连接
- oracle间隔分区
- 洛谷 P1556 幸福的路
- [React] Use the new React Context API
- bzoj4868: [Shoi2017]期末考试(三分法)
- bzoj1212: [HNOI2004]L语言(字典树)
- vue中响应式props办法
- Elasticsearch之重要核心概念(cluster(集群)、shards(分配)、replicas(索引副本)、recovery(据恢复或叫数据重新分布)、gateway(es索引的持久化存储方式)、discovery.zen(es的自动发现节点机制机制)、Transport(内部节点或集群与客户端的交互方式)、settings(修改索引库默认配置)和mappings)
- @Query Annotation in Spring Data JPA--转
- Hbase项目(完整版)