Applications are driven by state. Many things, like the user interface, should always be consistent with that state.
MobX is a general purpose FRP library that provides the means to derive, for example, a React based user interface automatically from the state and keep it synchronized.

The net result of this approach is that writing applications becomes really straight-forward and boilerplate free.

To synchronize the rendering of the state, we are going to use two functions from MobX. The first one is observable. We use observable to decorate our state to count attributes. We say, "MobX, please track this value, so that you can update observers whenever needed."

Next, we mark our component with the observer decorator from the MobX React package. It simply tells MobX, "This component's rendering can be derived from the relevant observables. Do so whenever needed."

const {observable} = mobx;
const {observer} = mobxReact;
const {Component} = React; @observer class Counter extends Component{
@observable count = 0; render(){
return(
<div>
Counter: {this.count} <br/>
<button onClick={this.inc}>+</button>
<button onClick={this.dec}>-</button>
</div>
)
} inc = () => {
this.count++;
} dec = () => {
this.count--;
}
} ReactDOM.render(
<Counter />,
document.getElementById('app')
)

Also spreate the state with the component will also works:

const {observable} = mobx;
const {observer} = mobxReact;
const {Component} = React; const appState = observable({
count : 0
});
appState.inc = function(){
this.count++;
}
appState.dec = function(){
this.count--;
} @observer class Counter extends Component{
render(){
return(
<div>
Counter: {this.props.store.count} <br/>
<button onClick={this.inc}>+</button>
<button onClick={this.dec}>-</button>
</div>
)
} inc = () => {
this.props.store.inc();
} dec = () => {
this.props.store.dec();
}
} ReactDOM.render(
<Counter store={appState}/>,
document.getElementById('app')
)

[React + Mobx] Mobx and React intro: syncing the UI with the app state using observable and observer的更多相关文章

  1. react使用mobx

    mobx api 使用装饰器语法 mobx数据转化为js数据 安装 yarn add mobx mobx-react yarn add babel-preset-mobx --dev "pr ...

  2. [Web] How to Test React and MobX with Jest

    转载自: https://semaphoreci.com/community/tutorials/how-to-test-react-and-mobx-with-jest?utm_content=bu ...

  3. react起步——从零开始编写react项目

    # index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...

  4. React学习之一:React初探

    一,React简介 React是由Facebook和Instagram开发的一套创建用户界面的JavaScript库.许多人认为React是MVC中的V. React创建的目的是为了:构建数据随时会改 ...

  5. 【react学习】关于react框架使用的一些细节要点的思考

    ( _(:3 」∠)_给园友们提个建议,无论是API文档还是书籍,一定要多看几遍!特别是隔一段时间后,会有意想不到的收获的)   这篇文章主要是写关于学习react中的一些自己的思考:   1.set ...

  6. React Native 系列(二) -- React入门知识

    前言 本系列是基于React Native版本号0.44.3写的,最初学习React Native的时候,完全没有接触过React和JS,本文的目的是为了给那些JS和React小白提供一个快速入门,让 ...

  7. React-Native(三):React Native是基于React设计的

    React Native是基于React js设计的. 参考:<React 入门实例教程> React 起源于 Facebook 的内部项目,因为该公司对市场上所有 JavaScript ...

  8. 1. React介绍 React开发环境搭建 React第一个程序

    什么是 React         React 是 Facebook 发布的 JavaScript 库,以其高性能和独特的设计理念受到了广泛关注. React的开发背景         Faceboo ...

  9. 转载 React.createClass 对决 extends React.Component

    先给出结论,这其实是殊途同归的两种方式.过去我们一般都会使用 React.createClass 方法来创建组件,但基于 ES6 的小小语法糖,我们还可以通过 extends React.Compon ...

随机推荐

  1. mysql备份sql,脚本

    MySQL 安装位置:/usr/local/mysq 论坛数据库名称为:bbs MySQL root 密码:123456 数据库备份目的地:/var/db_backup/ #! /bin/bash / ...

  2. [Linux]Ubuntu下如何将普通用户提升到root权限

    转至:http://jingyan.baidu.com/album/6181c3e0780131152ef153ff.html?picindex=0&qq-pf-to=pcqq.c2c  在u ...

  3. [网络编程] TCP、UDP区别以及TCP传输原理、拥塞避免、连接建立、连接释放总结

    TCP.UDP都是属于运输层的协议,提供端到端的进程之间的逻辑通信,而IP协议(网络层)是提供主机间的逻辑通信,应用层规定应用进程在通信时所遵循的协议.一.UDP主要特点:传输的是用户数据报协议.1. ...

  4. 在HTML页面布局中,position的值有几种,默然的值是什么

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  5. Linux内核监控模块-3-系统调用的截获

    上一章,我们获取了系统调用表的地址,这里我们来搞点所谓“截获”的事情.所谓“截获”即是将系统调用表里的地址指向我们自己写的一个函数,系统调用先执行我们自己写的函数,处理完后,再返回原来系统调用的执行函 ...

  6. Contest20140709 testA 树型DP

    testA 输入文件: testA.in 输出文件testA.out 时限5000ms 问题描述: 一棵树N个节点,每条边有一个距W,现在定义SUM为所有dist(X,Y)的和1<=X<Y ...

  7. jsconsole

    在移动设备或者其他无法启动 chrome developer tools 的时候可以用以下方法进行console 步骤: 1. 进入 http://jsconsole.com 的console画面,在 ...

  8. LeetCode解题报告:Reorder List

    Reorder List Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… Yo ...

  9. 【POJ】2001 Shortest Prefixes

    字典树. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 26 typed ...

  10. bzoj2809

    可以先穷举那个是管理者,然后就发现其实就是求每个子树选尽可能多的人,使薪水和小于m这显然是从小往大选,可以用启发式合并但是用主席树写的更简单一点吧,dfs序之后每课线段树不仅维护出现出现个数,然后在维 ...