[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 关键字
转自:http://www.cnblogs.com/hongten/p/hongten_python_keywords.html python3.3.2中的关键字如下: The following i ...
- 题解 P1337 【[JSOI2004]平衡点 / 吊打XXX】
这道题调了好久,果然非洲人是得不到眷顾的吗... 本题采用模拟退火解决. 模拟退火是一种简洁明了而又高效的近似算法,基本上可以套到任何求最优解的题目上去. 它的原理是模拟物理中金属退火的现象,凭借选手 ...
- js Math常用方法
------------------------ 向上取整,有小数就整数部分加1 Math.ceil(5/2) ------------------------ 四舍五入. Math.round(5/ ...
- Chrome插件Axure RP Extension
Chrome插件Axure RP Extension 1.将文件夹“0.6.2_0”复制到Chrome文件夹中某个位置. 2.打开Chrome,打开[设置] - [扩展程序],勾选右上角的“开发者模式 ...
- pcap网络抓包 无法import pcap
坑爹的不知道从哪里看到说仅仅有pcap最多仅仅支持到python2.5,然后又是easy install又是安装pip就是无法成功import pcap... 我的python版本号是2.7.8. s ...
- FZU_Problem 2168 防守阵地 I
Problem 2168 防守阵地 I Accept: 128 Submit: 392 Time Limit: 3000 mSec Memory Limit : 32768 KB Problem De ...
- 从头认识Spring-3.4 简单的AOP日志实现-扩展添加检查订单功能,以便记录并检測输入的參数
这一章节我们再上一个章节的基础上加上一个检查订单功能 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03.topic_1 ...
- hdu 4966 最小树形图
将每门课等级拆成0,1,2,3...a[i]个点,对每一个等级大于0的点向它低一级连边,权值为0[意思是,若修了level k.则level(0~k)都当做修了] 将输入的边建边,权值为money[i ...
- pgsql数据库备份还原记
今天又搞了一个pgsql 的备份还原,差一点没有成功,以前总是想当然的用,没认真想背后的东西,也没对过程中的疑问做记录,所以后面也没什么印象,常见常新,这次既然又遇到就总结一下. 之前操作pgsql数 ...
- 132.try throw catch介绍
#include <iostream> using namespace std; //try尝试执行,抛出throw,throw之后语句不再执行 //catch处理throw的异常 voi ...