zoukankan      html  css  js  c++  java
  • [MST] Describe Your Application Domain Using mobx-state-tree(MST) Models

    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(1)
        expect(list.items[0].price).toBe(28.73)
    })
  • 相关阅读:
    Python-按指定列排序、斜着遍历
    牛客-SQL-刷题(下)
    特征工程之离散变量处理
    python 原生列表删除元素方法总结
    sklearn进行归一化
    Keras安装与测试遇到的坑
    常用机器学习算法优缺点及应用汇总
    特征工程
    机器学习模型评估指标总结
    pyecharts多图表同一页显示
  • 原文地址:https://www.cnblogs.com/Answer1215/p/8337570.html
Copyright © 2011-2022 走看看