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. 学习hamcrest和mockito时的总结和demo

    UT中需要的jar Junit4.1X.jar hamcrest-library-1.x.jar hamcrest-core-l.x.jar mockito-all-1.10.x.jar Junit ...

  2. sql server 数据库基础学习心得 思维导图

  3. Hadoop下各技术应用场景

    数据采集和DataFlow 对于数据采集主要分为三类,即结构化数据库采集,日志和文件采集,网页采集.对于结构化数据库,采用Sqoop是合适的,可以实现结构化数据库中数据并行批量入库到hdfs存储.对于 ...

  4. 【Java】WEB-INF目录与META-INF目录的作用

    /WEB-INF/web.xml Web应用程序配置文件,描述了 servlet 和其他的应用组件配置及命名规则. /WEB-INF/classes/包含了站点所有用的 class 文件,包括 ser ...

  5. Angular1.0

    公司会议室组织分享,两个小时困死我了,一点凌乱笔记: $http.get和promise一样有then方法,成功,失败 jquery each遍历对象i,n ng-app ng-controller ...

  6. ZooKeeper系列之八:ZooKeeper的简单操作

    http://blog.csdn.net/shenlan211314/article/details/6187035 1 )使用 ls 命令来查看当前 ZooKeeper 中所包含的内容: [zk: ...

  7. Jsp中获得集合List或Set的长度

    首先要引入<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> ...

  8. 【HDU3247】 Resource Archiver(DP+AC自动机+最短路)

    Resource Archiver Time Limit: 10000MS   Memory Limit: 100000KB   64bit IO Format: %I64d & %I64u ...

  9. [topcoder]UnsealTheSafe

    http://community.topcoder.com/stat?c=problem_statement&pm=4471&rd=10711 这题果然是道简单题,图+DP.拿道题便觉 ...

  10. 此集合已经采用方案 http 的地址。此集合中每个方案中最多只能包含一个地址。

    错误信息:此集合已经采用方案 http 的地址.此集合中每个方案中最多只能包含一个地址.如果服务承载于 IIS 中,则可以通过将“system.serviceModel/serviceHostingE ...