博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[MST] Build Forms with React to Edit mobx-state-tree Models
阅读量:5798 次
发布时间:2019-06-18

本文共 2606 字,大约阅读时间需要 8 分钟。

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 (            
  • ); } renderItemView(item) { return (
  • {item.image && }

    {item.name}

    {item.price}
  • ); } onSaveEdit = () => { applySnapshot(this.props.item, getSnapshot(this.state.clone)) this.setState({ isEditing: false, clone: null }) }; onCancelEdit = () => { this.setState({isEditing: false}) }; onToggleEdit = () => { this.setState({ isEditing: true, clone: clone(this.props.item) }) }}export default observer(WishListItemView)

     

    Here we have to use the methods provide from the lib, because the 'this.props.item' is a mobx state model object:

    {$mobx: ObservableObjectAdministration, toString: ƒ, …}

     

    The good part of this approach is that, the model related data can be handled by the mobx-state-tree lib. It can helps to udpate the model efficiently.

    The down side of the approach is that,  you needs to keep at least tow parts of state, one is mobx-state-tree, another one is the component state, for example, in the code, 'isEiditing' & 'clone'. 

     

    转载地址:http://amifx.baihongyu.com/

    你可能感兴趣的文章
    L104
    查看>>
    做完小程序项目、老板给我加了6k薪资~
    查看>>
    脱离“体验”和“安全”谈盈利的游戏运营 都是耍流氓
    查看>>
    TortoiseSVN中图标的含义
    查看>>
    Python version 2.7 required, which was not foun...
    查看>>
    [BZOJ] 1012 [JSOI2008]最大数maxnumber
    查看>>
    根据毫秒数计算出当前的“年/月/日/时/分/秒/星期”并不是件容易的事
    查看>>
    Unity Shaders and Effects Cookbook (3-5) 金属软高光
    查看>>
    31-hadoop-hbase-mapreduce操作hbase
    查看>>
    NYOJ283对称排序
    查看>>
    C#反射实例应用--------获取程序集信息和通过类名创建类实例
    查看>>
    VC中实现文字竖排的简单方法
    查看>>
    程序员常用的六大技术博客类
    查看>>
    深入理解浏览器的缓存机制
    查看>>
    又拍云沈志华:如何打造一款安全的App
    查看>>
    dubbo源码分析-架构
    查看>>
    6套毕业设计PPT模板拯救你的毕业答辩
    查看>>
    Windows phone 8 学习笔记
    查看>>
    我的友情链接
    查看>>
    sshd_config设置参数笔记
    查看>>