[React + Mobx] Mobx and React intro: syncing the UI with the app state using observable and observer
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的更多相关文章
- react使用mobx
mobx api 使用装饰器语法 mobx数据转化为js数据 安装 yarn add mobx mobx-react yarn add babel-preset-mobx --dev "pr ...
- [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 ...
- react起步——从零开始编写react项目
# index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...
- React学习之一:React初探
一,React简介 React是由Facebook和Instagram开发的一套创建用户界面的JavaScript库.许多人认为React是MVC中的V. React创建的目的是为了:构建数据随时会改 ...
- 【react学习】关于react框架使用的一些细节要点的思考
( _(:3 」∠)_给园友们提个建议,无论是API文档还是书籍,一定要多看几遍!特别是隔一段时间后,会有意想不到的收获的) 这篇文章主要是写关于学习react中的一些自己的思考: 1.set ...
- React Native 系列(二) -- React入门知识
前言 本系列是基于React Native版本号0.44.3写的,最初学习React Native的时候,完全没有接触过React和JS,本文的目的是为了给那些JS和React小白提供一个快速入门,让 ...
- React-Native(三):React Native是基于React设计的
React Native是基于React js设计的. 参考:<React 入门实例教程> React 起源于 Facebook 的内部项目,因为该公司对市场上所有 JavaScript ...
- 1. React介绍 React开发环境搭建 React第一个程序
什么是 React React 是 Facebook 发布的 JavaScript 库,以其高性能和独特的设计理念受到了广泛关注. React的开发背景 Faceboo ...
- 转载 React.createClass 对决 extends React.Component
先给出结论,这其实是殊途同归的两种方式.过去我们一般都会使用 React.createClass 方法来创建组件,但基于 ES6 的小小语法糖,我们还可以通过 extends React.Compon ...
随机推荐
- 2016最新Java笔试题集锦
更新时间:2015-08-13 来源:网络 投诉删除 [看准网(Kanzhun.com)]笔试题目频道小编搜集的范文“2016最新Java笔试题集锦”,供大家阅读参考, ...
- Spring MVC和Struts2的区别
1. 机制:spring mvc的入口是servlet,而struts2是filter,这样就导致了二者的机制不同. 2. 性能:spring会稍微比struts快.spring mvc是基于方法的设 ...
- JS匿名执行函数
一.匿名函数的创建 第一种:(调用sum后可执行) var sum=function(x,y){ return x+y; } 第二种:(可自执行) void function(x,y){ }(1,2) ...
- 开启Eclipse 智能感知代码功能
1.打开windows->Perferences..窗口,选择java->Editor->Content Assist,在右下方的“Auto Activation triggers ...
- Unity 截取图片并且显示出来
Unity 截取图片并且显示出来 近期用到了unity的截图,并且把他显示出来,摸索了很多! 讲解一个东西,android可以存储一些文件在本地,比如配置文件等! 对于不同的系统,方法不一! if ( ...
- PHP 7 探针的安装与测试
首先,这是一篇软文,没错!就是一篇软文.因为我知道「PHP 是世界上最好的语言」,所以我相信很多人愿意读这篇用户帮忙写的软文,因为这篇软文应该能帮助 PHP 开发的同学一点小忙.我们是一家成立了7年的 ...
- linux平台的office文档转pdf(程序员的菜)
需要材料: 1. Openoffice3.4(我是32位的centos,可以根据自己的系统下载指定的openoffice软件包) 下载地址:http://sourceforge.net/projec ...
- java中的日期格式
时间日期标识符: yyyy:年 MM:月 dd:日 hh:~12小时制(-) HH:24小时制(-) mm:分 ss:秒 S:毫秒 E:星期几 D:一年中的第几天 F:一月中的第几个星期(会把这个月总 ...
- RemixOS Player 让用户在 Windows 上运行 Android App
http://www.oschina.net/news/77154/remixos-player-windows-andriod-app
- Zlib压缩算法在Java与Delphi间交互实现(压缩XML交互)
一个典型应用中,使用delphi作为客户端,J2EE服务端,两者之间用XML作为数据交换,为了提高效率,对XML数据进行压缩,为此需要找到一种压缩/解压算法能够两个平台之间交互处理,使用ZLIB算法就 ...