[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 ...
随机推荐
- WPF 核心体系结构
WPF 体系结构 主题提供 Windows Presentation Foundation (WPF) 类层次结构,涵盖了 WPF 的大部分主要子系统,并描述它们是如何交互的. System.Obje ...
- 苹果搜索广告后台大揭秘,最全最细致详解,手把手设置教程「后附官方视频」-b
WWDC2016 搜索广告分会视频和 PPT 发布了,ASO100 带开发者第一时间了解 Search Ads 后台设置(文末有原声视频). 首先介绍一下搜索广告的模式和竞价规则 广告模式为 CPT( ...
- Node.js V0.12新特性之性能优化
v0.12悠长的开发周期(已经过去九个月了,并且还在继续,是有史以来最长的一次)让核心团队和贡献者们有充分的机会对性能做一些优化.本文会介绍其中最值得注意的几个. 支持塞住模式的可写流 现在可写流可以 ...
- 用正则式判断URL是否合法-python
import sys import re #Make sure we have a single URL argument. if len(sys.argv) != 2: print (sys.std ...
- SQL Server ->> 深入探讨SQL Server 2016新特性之 --- Temporal Table(历史表)
原文:SQL Server ->> 深入探讨SQL Server 2016新特性之 --- Temporal Table(历史表) 作为SQL Server 2016(CTP3.x)的另一 ...
- Android 用代码来实现selector
众所周知,android可以通过XML文件来创建selector,以Drawable对象的形式安装到组件上,以提供统一的风格设置.但是在某些时候,我们需要通过代码的形式来实现相同的功能,例如组件数量非 ...
- 开发备必:WEB前端开发规范文档
为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档. 本规范文档一经确认, 前端开发人员必 须按本文档规范进行前台页面开发. 本文档如有不对或者不合适的地 ...
- IP定位 C#
IP定位 已经不是什么新的技术,但是在做项目中却会常常用到.找网上找了许久,也做了许多的实验,觉得QQwry.dat,很很好用的,作者也提供了开发的源码和大家分享. 在这里感谢作者.我在项目中也用到了 ...
- Json 的日期格式转换成DateTime
JSON 的日期形式:”/Date(1242357713797+0800)/” , 下面我们就用以下C#的方法将他转换成DateTime类型: /// <summary> /// Json ...
- UVa 11077 Find the Permutations(置换+递推)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35431 [思路] 置换+递推 将一个排列看作一个置换,分解为k个循 ...