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)
})

最新文章

  1. 安装SQL SERVER 2005出现“sql2005 执行安装向导期间出错 ”
  2. jsp日期插件My97DatePicker
  3. linux下如何查找需要的文件后并删除
  4. X86 架构和 ARM 架构
  5. css基础学习
  6. C#模拟POST提交表单(二)--HttpWebRequest以及HttpWebResponse
  7. BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊(动态树)
  8. 在没有DOM操作的日子里,我是怎么熬过来的(中)
  9. monkey事件简介
  10. windows配置Erlang环境
  11. springBoot项目启动类启动无法访问
  12. geeksforgeeks-Array-Rotation and deletion
  13. 图->定义
  14. JavaScript Transpilers: 为什么需要用它们?Babel的使用介绍。
  15. docker网络之overlay
  16. windows10中远程登录ubuntu16.04的桌面
  17. 关于Android中传递数据的一些讨论
  18. android camera preview常用格式
  19. pythong 中的 __call__
  20. HDU 1317 XYZZY(floyd+bellman_ford判环)

热门文章

  1. C#WIFI搜索与连接
  2. oracle间隔分区
  3. 洛谷 P1556 幸福的路
  4. [React] Use the new React Context API
  5. bzoj4868: [Shoi2017]期末考试(三分法)
  6. bzoj1212: [HNOI2004]L语言(字典树)
  7. vue中响应式props办法
  8. Elasticsearch之重要核心概念(cluster(集群)、shards(分配)、replicas(索引副本)、recovery(据恢复或叫数据重新分布)、gateway(es索引的持久化存储方式)、discovery.zen(es的自动发现节点机制机制)、Transport(内部节点或集群与客户端的交互方式)、settings(修改索引库默认配置)和mappings)
  9. @Query Annotation in Spring Data JPA--转
  10. Hbase项目(完整版)