[MST] Build Forms with React to Edit mobx-state-tree Models
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
cloneto create a full, deep clone of any model instance - Using
applySnapshotto 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的更多相关文章
- 从零配置webpack(react+less+typescript+mobx)
本文目标 从零搭建出一套支持react+less+typescript+mobx的webpack配置 最简化webpack配置 首页要初始化yarn和安装webpack的依赖 yarn init -y ...
- [Web 前端] mobx教程(三)-在React中使用Mobx
copy from : https://blog.csdn.net/smk108/article/details/85053903 Mobx提供了一个mobx-react包帮助开发者方便地在React ...
- Build your own React
Build your own React https://pomb.us/build-your-own-react/ https://github.com/pomber/didact demo htt ...
- [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 ...
- React使用DVA本地state传值取值
React使用DVA本地state传值取值 最近在用Ant Pro 做一个后台系统,在使用中发现Antd Pro使用DVA来实现redux+sagas+router一系列的功能,比传统方式要方便快捷的 ...
- [转] 深入理解React 组件状态(State)
React 的核心思想是组件化的思想,应用由组件搭建而成,而组件中最重要的概念是State(状态),State是一个组件的UI数据模型,是组件渲染时的数据依据. 一. 如何定义State 定义一个合适 ...
- React中Props 和 State用法
React中Props 和 State用法 1.本质 一句话概括,props 是组件对外的接口,state 是组件对内的接口.组件内可以引用其他组件,组件之间的引用形成了一个树状结构(组件树),如果下 ...
- 深入理解React 组件状态(State)
React 的核心思想是组件化的思想,应用由组件搭建而成,而组件中最重要的概念是State(状态),State是一个组件的UI数据模型,是组件渲染时的数据依据. 一. 如何定义State 定义一个合适 ...
- React Native中Mobx的使用
从今天开始我们来搞搞状态管理可否,这几天没怎么写博客,因为被病魔战胜了,tmd,突然的降温让我不知所措,大家最近注意安全,毕竟年底了,查的严,呸,大家注意保暖 特别声明:写该文只是写一下用MobX的思 ...
随机推荐
- 关于一些运算(&(与运算)、|(或运算)、^(异或运算)........)的本质理解【转】
看到一篇博客,关于一些运算的解析,觉得有用,怕以后找不着,直接复制下来,以备以后学习用 原文链接:https://blog.csdn.net/xiaopihaierletian/article/det ...
- static和extern的作用域--题目
#include <stdio.h> ; int main(void) { , sum = , count = ; ,count++) // count = 2 { ; count++; ...
- js判断 nan null undefined的方法
收集资料如下判断: 1.判断undefined: 复制代码代码如下: <span style="font-size: small;">var tmp = undefin ...
- 【转载】黑客内核:编写属于你的第一个Linux内核模块
黑客内核:编写属于你的第一个Linux内核模块
- 零基础学python-5.2 数字表达式操作符
表达式是处理数字最主要的工具 a=1#常量 a=a+1#表达式 操作符 操作符 描写叙述 yield 生成 器函数发送协议 lambda args:expression 生成匿名函数 x if y e ...
- JSP简单练习-上传文件
注意:在编写上传文件的代码时,需确保"WEB-INF/lib"下含有jspsmartupload.jar包.否则会出错. jspSmartupload.jar下载 <!-- ...
- 弹性ScrollView,和下啦刷新的效果相似 实现下拉弹回和上拉弹回
今天做了一个弹性ScrollView,和下啦刷新的效果类似,我想这个非常多需求都用的这样的效果 事实上这是一个自己定义的scrollView,上代码.这是我写在一个公共的组件包里的 package c ...
- [jzoj 4879] [NOIP2016提高A组集训第11场11.9] 少女觉 解题报告 (贪心)
题目链接: http://172.16.0.132/senior/#main/show/4879 题目: 在幽暗的地灵殿中,居住着一位少女,名为古明地觉.据说,从来没有人敢踏入过那座地灵殿,因为人们恐 ...
- 移动端video播放时不弹出页面层
移动端视频在播放时会主动弹出页面,有的浏览器不会.对那些会的浏览器进行处理: 直接加上下面三个属性即可,兼容方面就不说了,微信上是很ok的. <video x5-playsinline=&quo ...
- python网页问题
#django-admin不是个命令 添加环境变量 D:\Python36\Scripts #localhost加载失败 命令行 python manage.py runserver 0.0.0.0: ...