[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)
[MST] Create an Entry Form to Add Models to the State Tree的更多相关文章
- 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-Prog ... 
- SharePoint自动化系列——Create a local user and add to SharePoint
		转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 实现过程:在本地创建一个local user并将该user添加到Administrators组中, ... 
- Mongodb之failed to create service entry worker thread
		Mongodb "failed to create service entry worker thread" 错误. 系统:CentOS release 6.8 mongod.lo ... 
- Start state is missing. Add at least one state to the flow
		springmvc配置过程中,会配置数据库文件,比如说如下文件:这个时候可能会出现“Start state is missing. Add at least one state to the flow ... 
- springmvc.xml 中报错:Start state is missing. Add at least one state to the flow
		最近一个学弟问我关于整合springMVC和spring出现的配置文件springmvc.xml出现的Start state is missing. Add at least one state to ... 
- create index 与 alter table add index 区别
		众所周知,MySQL创建索引有两种语法,即:ALTER TABLE HeadOfState ADD INDEX (LastName, FirstName);CREATE INDEX index_nam ... 
- [MST] Create Dynamic Types and use Type Composition to Extract Common Functionality
		Since MST offers a runtime type system, it can create and compose types on the fly, making it possib ... 
- how to create a custom form for sharepoint list
		在VS中创建一个applicationPage映射到Layouts文件夹下,然后代码如下: SPList lstTest = web.Lists["Shared Documents" ... 
- python---django中form组件(1)简单使用和字段了解
		Django中的Form组件功能: 1.对用户请求的验证 2.生成html代码 Form使用:对用户请求进行验证 前端代码: <form action="/f1.html" ... 
随机推荐
- Python及相应软件安装
			Python安装 这是下载地址:Linux下载链接,windows下载链接 1.下载压缩包 wget https://www.python.org/ftp/python/3.7.1/Python-3. ... 
- 解决utf8' codec can't decode byte 0xe5 in position 0: unexpected end of data
			使用unicode对象的话,除了这样使用u标记,还可以使用unicode类以及字符串的encode和decode方法. unicode类的构造函数接受一个字符串参数和一个编码参数,将字符串封装为一个u ... 
- mycat的配置和学习
			1.mycat一共三个配置文件. 1)server.xml:配置逻辑库的名称访问的账号密码 <user name="root"> <property name=& ... 
- pip安装-mac电脑
			If you meant "pip" specifically: Homebrew provides pip via: `brew install python`. However ... 
- Android知识点总结
			说明 当中大部分文章都是转载自其它大神之手.在转载的过程中学到了非常多,这里主要解说的是android体系的相关知识点,本文会持续更新. 1 Android service相关知识点 Android ... 
- Codeforces Round #252 (Div. 2)-C,D
			C题就是一个简单的模拟.首先给每一个人两个.然后把剩下的都给一个人就好了. 给的时候蛇形给. #include<stdio.h> #include<string.h> #inc ... 
- iOS开发之十万个为什么<1>
			郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助.欢迎给作者捐赠,支持郝萌主,捐赠数额任意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 游戏官方下 ... 
- JAVA设计模式之【观察者模式】
			观察者模式 交通信号灯是汽车的观察目标,汽车是观察者 一个对象的状态或行为的变化将导致其他对象的状态或行为也发生变化 为了描述这种一对多或一对一的联动,观察者模式应运而生 在观察者模式中,发生改变的对 ... 
- correct ways to define variables in python
			http://stackoverflow.com/questions/9056957/correct-way-to-define-class-variables-in-python later say ... 
- 11.使用boostregex遭遇无法打开libboost_regex-vc120-mt-sgd-1_62.lib的问题
			通过Boost库可以在C++项目中使用正则表达式,配置好环境后链接过程出现”无法打开libboost_regex-vc120-mt-sgd-1_62.lib”的错误. 原因是按照官方生成lib的方法 ... 
