[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‘ve built so far.

In this lesson you will learn:

  • MST is not limited to a single state tree. Every model can form a tree on its own
  • Appending model instances to the state tree

New entry component can be a stateless component, using State Model to create an empty entry, just for setting default value.

Once add button was clicked, fire a callback to add new node tor the tree.

import React, {Component} from ‘react‘;
import {observer} from ‘mobx-react‘;

import WishListItemEdit from ‘./WishListItemEdit‘;
import {WishListItem} from ‘../models/WishList‘;

class WishListItemEntry extends Component {
    constructor() {
        super();
        // create an empty entry
        this.state = {
            entry: WishListItem.create({
                name: ‘‘,
                price: 0
            })
        }
    }

    render() {
        return (
            <div>
                <WishListItemEdit item={this.state.entry} />
                <button onClick={this.onAdd}>Add</button>
            </div>
        );
    }

    onAdd = () => {
        // call the CB
        this.props.onAdded(this.state.entry);
        // clean the name and price
        this.setState({
            entry: WishListItem.create({name: ‘‘, price: 0})
        })
    }
}

export default observer(WishListItemEntry);

WishListListView.js

import React, {Component} from "react"
import { observer } from "mobx-react"

import WishListItemView from "./WishListItemView"
import WishListItemEntry from ‘./WishListItemEntry‘;

class WishListView extends Component {
   render() {
       const {wishList} = this.props;
       return (
           <div className="list">
               <ul>{wishList.items.map((item, idx) => <WishListItemView key={idx} item={item} />)}</ul>
               Total: {wishList.totalPrice} €
               <WishListItemEntry onAdded={this.onItemAdded}/>
           </div>
       );
   }

    onItemAdded = (entry) => {
       if(entry) {
           this.props.wishList.add(entry);
       }
    }
}

export default observer(WishListView)

原文地址:https://www.cnblogs.com/Answer1215/p/8372721.html

时间: 2024-10-09 22:33:28

[MST] Create an Entry Form to Add Models to the State Tree的相关文章

Create Custom Modification Form In VS 2012-Part1

Step1.Add EventHandlingScope Activity Under OnWorkflowActivated Step2.Add SequenceActivity In EventHandlingScope Activity Step3.Add EnableWorkflowModification Activity a.Bind [ContextData] to a new field b.Set [Colorrelation Token] as "Modification&q

How to hide an entry in the Add/Remove Programs applet?

Original link: http://www.winhelponline.com/articles/15/1/How-to-hide-an-entry-in-the-AddRemove-Programs-applet.html ------------------------Following content is only for knowledge sharing. --------------------------- This article discusses the metho

SharePoint自动化系列——Create a local user and add to SharePoint

实现过程:在本地创建一个local user并将该user添加到Administrators组中,然后在SharePoint指定site中添加该user,并赋予Full Control的权限. 脚本如下: function AddUserToSPSite { param($siteUrl,$userName,$pwd,$fullName,$dspt) #Create a local user and add to a local group. try{ $computer = [ADSI]"Wi

Create Custom Modification Form In VS 2012-Part2

1.SPWorkflowModification ContextData is XMLSerialized as String. 2.Get SPWorkflowModification ContextData in modification page protected void GetContexData()        {            SPWeb currentWeb = SPContext.Current.Web;            string strWorkflowI

create index 与 alter table add index 区别

众所周知,MySQL创建索引有两种语法,即:ALTER TABLE HeadOfState ADD INDEX (LastName, FirstName);CREATE INDEX index_name HeadOfState (LastName, FirstName);那么,这两种语法有什么区别呢? 在网上找了一下,在一个英文网站上,总结了下面几个区别,我翻译出来,如下:1.CREATE INDEX必须提供索引名,对于ALTER TABLE,将会自动创建,如果你不提供:2.CREATE IND

[NOIp]二叉树的指针实现

今天学到了二叉树,一开始被那个malloc弄的很迷,后来发现root=(BiTreeNode*)malloc(sizeof(BiTreeNode))的那个星号是在后面的,吐血.. 代码里面有个小技巧,就是typedef struct XXX{...}XXX,这样就使用XXX代替了struct XXX,可以少打一些字了233. #include<bits/stdc++.h> using namespace std; typedef struct BiTreeNode { int data; Bi

Redux系列x:源码解析

写在前面 redux的源码很简洁,除了applyMiddleware比较绕难以理解外,大部分还是 这里假设读者对redux有一定了解,就不科普redux的概念和API啥的啦,这部分建议直接看官方文档. 此外,源码解析的中文批注版已上传至github,可点击查看.本文相关示例代码,可点击查看. 源码解析概览 将redux下载下来,然后看下他的目录结构. npm install redux 这里我们需要关心的主要是src目录,源码解析需要关心的文件都在这里面了 index.js:redux主文件,主

redux源码解析,函数式编程

提到redux,会想到函数式编程.什么是函数式编程?是一种很奇妙的函数式的编程方法.你会感觉函数式编程这么简单,但是用起来却很方便很神奇. 在<functional javascript>中,作者批评了java那种任何东西都用对象来写程序的方式,提倡了这种函数式编程. 之前看过一些函数式编程的例子(以下简称fp).提到fp会想到underscore和lodash,你会看到lodash的包中,唯一一个文件夹就是fp,里面是fp相关的函数. 在redux中,也是运用了很多fp的函数.其实在写js中

Oracle Form Data Entry Sample

I shared a data entry example form here in this post for Oracle Forms beginner developers, so that they may take an idea to how to develop a simple data entry form in Oracle Forms. The form can be fully modified. Example is given for an emp table and