We will expand our UI, and give the user the possibility to edit his wishlist. We will use the earlier defined actions. We also will use model clones only to modify data temporarily, and after approving the changes, apply them back to the original model.

In this lesson you will learn:

  • How to call model actions from a component
  • Using clone to create a full, deep clone of any model instance
  • Using applySnapshot to update the state of a model instance given a snapshot.

The whole point for building a editing form component is that:

1. avoid two ways data flow, means that you change the data inside the form, without saving but the data was mutated already. To solve this problem, we will use 'clone' from 'mobx-state-tree'.

2. When save the data, we can use 'getSnapshot' and 'applySnapshot' from the lib.

import React, {Component} from "react"
import {observer} from "mobx-react"; import {clone, getSnapshot, applySnapshot} from 'mobx-state-tree'; import WishListItemEdit from "./WishListItemEdit" class WishListItemView extends Component {
constructor() {
super(); this.state = {isEditing: false}
} render() {
const {item} = this.props;
return this.state.isEditing ?
this.renderEditable() :
this.renderItemView(item);
} renderEditable() {
return (
<li className="item">
<WishListItemEdit item={this.state.clone}/>
<button onClick={this.onSaveEdit}>

[MST] Build Forms with React to Edit mobx-state-tree Models的更多相关文章

  1. 从零配置webpack(react+less+typescript+mobx)

    本文目标 从零搭建出一套支持react+less+typescript+mobx的webpack配置 最简化webpack配置 首页要初始化yarn和安装webpack的依赖 yarn init -y ...

  2. [Web 前端] mobx教程(三)-在React中使用Mobx

    copy from : https://blog.csdn.net/smk108/article/details/85053903 Mobx提供了一个mobx-react包帮助开发者方便地在React ...

  3. Build your own React

    Build your own React https://pomb.us/build-your-own-react/ https://github.com/pomber/didact demo htt ...

  4. [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'v ...

  5. React使用DVA本地state传值取值

    React使用DVA本地state传值取值 最近在用Ant Pro 做一个后台系统,在使用中发现Antd Pro使用DVA来实现redux+sagas+router一系列的功能,比传统方式要方便快捷的 ...

  6. [转] 深入理解React 组件状态(State)

    React 的核心思想是组件化的思想,应用由组件搭建而成,而组件中最重要的概念是State(状态),State是一个组件的UI数据模型,是组件渲染时的数据依据. 一. 如何定义State 定义一个合适 ...

  7. React中Props 和 State用法

    React中Props 和 State用法 1.本质 一句话概括,props 是组件对外的接口,state 是组件对内的接口.组件内可以引用其他组件,组件之间的引用形成了一个树状结构(组件树),如果下 ...

  8. 深入理解React 组件状态(State)

    React 的核心思想是组件化的思想,应用由组件搭建而成,而组件中最重要的概念是State(状态),State是一个组件的UI数据模型,是组件渲染时的数据依据. 一. 如何定义State 定义一个合适 ...

  9. React Native中Mobx的使用

    从今天开始我们来搞搞状态管理可否,这几天没怎么写博客,因为被病魔战胜了,tmd,突然的降温让我不知所措,大家最近注意安全,毕竟年底了,查的严,呸,大家注意保暖 特别声明:写该文只是写一下用MobX的思 ...

随机推荐

  1. ASP.NET-Microsoft.Management.Infrastructure错误

    错误如图所示,将MVC发布到IIS上就会出现这个错误,我用到了NPOI这个EXCEL插件,不知道是不是这个造成的,但是实在找不到解决方案,就直接将BIN目录下的这个Microsoft.Manageme ...

  2. [cocos2dx笔记013]一个使用CCRenderTexture创建动态纹理显示数字的类

    用CCLabelTTF显示的数字不好看.于是就想到用图片来代理.眼下网上的实现都是把每一个数字做一个CCSprite组合的方式. 可是我想.动态生成纹理的方式.没有就仅仅好自己手动写一个. 头文件 # ...

  3. 【动态树问题】LCT学习笔记

    我居然还不会LCT QAQ真是太弱了 必须学LCT QAQ ------------------线割分是我www------------ LinkCut-Tree是基于Splay(由于Splay能够非 ...

  4. android选择图片或拍照图片上传到server(包含上传參数)

    在9ria论坛看到的.还没測试,先Mark与大家分享一下. 近期要搞一个项目,须要上传相冊和拍照的图片.不负所望,最终完毕了! 只是须要说明一下,事实上网上非常多教程拍照的图片.都是缩略图不是非常清晰 ...

  5. iOS开发实践之xib载入注意问题

    xib都会addSubview加入到控制器view中时程序崩溃.错误提示: 'NSInvalidArgumentException', reason: '-[ UITapGestureRecogniz ...

  6. 手动配置IP网络

    1. #vi /etc/sysconfig/network-scripts/ifcfg-ens33 ifcfg-ens33 是网卡的名字,根据自己的计算机 2. 修改配置 ONBOOT=no改为ONB ...

  7. 17.广度优先遍历bfs

    #include <iostream> #include <boost/config.hpp> //图(矩阵实现) #include <boost/graph/adjac ...

  8. 内存文件系统:tachyon(现在叫Alluxio)

    此文于2015 年 8 月 10 日发布 Tachyon 是什么 Tachyon 是 AMPLab 开发的一款内存分布式文件系统.它介于计算层和存储层之间,可以简单的理解为存储层在内存内的一个 Cac ...

  9. (转载)自定义View——弹性滑动

    滑动是Android开发中非常重要的UI效果,几乎所有应用都包含了滑动效果,而本文将对滑动的使用以及原理进行介绍. 一.scrollTo与ScrollBy View提供了专门的方法用于实现滑动效果,分 ...

  10. Sublime Text 3 注册码 激活码 版本号 Build 3143

    —– BEGIN LICENSE —– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD93 4DCBA022 FAF60790 61AA ...