[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的思 ...
随机推荐
- Cygwin(类UNIX模拟环境)&CURL(强大的http命令行工具)
前言: 需要我用curl试下能否发送post请求调起公司的仿真系统(目前) 跟着大佬的脚步,亲测一把~ 感谢大佬的提供的博客和指导 @咩神 个人博客园及来源地址 Cygwin(类UNIX模拟环境) 一 ...
- js 学习思维导图
- ACdream 1157 Segments
Segments Time Limit: 2000ms Memory Limit: 10000KB This problem will be judged on ACdream. Original I ...
- [POJ3233]Matrix Power Series 分治+矩阵
本文为博主原创文章,欢迎转载,请注明出处 www.cnblogs.com/yangyaojia [POJ3233]Matrix Power Series 分治+矩阵 题目大意 A为n×n(n<= ...
- 第九章 Servlet API
第九章 Servlet API Servlet API 定义了Servlet和服务器之间的一个标准接口,这使得Servlet具有跨应用服务器的特性,通过使用Servlet API,开发人员不必关心服务 ...
- 洛谷 P2071 座位安排 seat.cpp/c/pas
P2071 座位安排 seat.cpp/c/pas 题目背景 公元二零一四年四月十七日,小明参加了省赛,在一路上,他遇到了许多问题,请你帮他解决. 题目描述 已知车上有N排座位,有N*2个人参加省赛, ...
- sqlite学习笔记11:C语言中使用sqlite之删除记录
最后一节,这里记录下怎样删除数据. 前面全部的代码都继承在这里了,在Ubuntu14.04和Mac10.9上亲測通过. #include <stdio.h> #include <st ...
- LIVE555研究之五:RTPServer(二)
port是一样的. DynamicRTSPServer 继承关系: Medium是非常多类的基类.内部定义了指向环境类的引用和一个char类型媒体名称.并定义了依照媒体名称,查找相应媒体的成员函数lo ...
- zzulioj--1791-- 旋转矩阵(模拟水题)
旋转矩阵 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 268 Solved: 116 SubmitStatusWeb Board Descr ...
- hdoj--1016--Prime Ring Problem(递归回溯)
Prime Ring Problem Time ...