[Mobx] Using mobx to isolate a React component state
React is great for diffing between Virtual-DOM and rendering it to the dom. It also offers a naïve solution for diffing state in terms of setState. However it is slightly verbose and not easy to scale. MobX offers a very simple and effective solution to manage state in React components.
The whole to use Mobx is to sprate "Data logic" from 'DOM presentation'.
The state can be marked by '@observable'.
The function to dispatch action can be maked by '@action'.
The React class can be marked by '@observe'.
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { observable, action } from 'mobx';
import { observer } from 'mobx-react'; class HelloData {
@observable clickedCount = 0; @action
increment() {
this.clickedCount++;
}
} @observer
class Hello extends React.Component<{}> {
data = new HelloData();
render() {
return (
<button onClick={() => this.data.increment()}>
Click count = {this.data.clickedCount}
</button>
);
}
} ReactDOM.render(
<Hello />,
document.getElementById('root')
);
[Mobx] Using mobx to isolate a React component state的更多相关文章
- React.Component 与 React.PureComponent(React之性能优化)
前言 先说说 shouldComponentUpdate 提起React.PureComponent,我们还要从一个生命周期函数 shouldComponentUpdate 说起,从函数名字我们就能看 ...
- [React] Update Component State in React With Ramda Lenses
In this lesson, we'll refactor a React component to use Ramda lenses to update our component state. ...
- [React] Update State in React with Ramda's Evolve
In this lesson we'll take a stateful React component and look at how we can refactor our setState ca ...
- React给state赋值的两种写法
如果你看过React的官方文档,就会对怎么给局部state赋值有一定的了解.如下代码: class Test extends React.Component { constructor(props) ...
- 七天接手react项目 —— state&事件处理&ref
state&事件处理&ref 在 react 起步 一文中,我们学习了 react 相关知识:jsx.组件.props.本篇将继续研究 state.事件处理和ref. state St ...
- 【MobX】MobX 简单入门教程
一.MobX 介绍 首先看下官网介绍: MobX 是一个经过战火洗礼的库,它通过透明的函数响应式编程(transparently applying functional reactive progra ...
- 学习React系列(一)——React.Component 生命周期
挂载中(只执行一次) 以下方法在组件实例正被创建和插入到DOM中时调用 constructor()一般用于初始化state和方法的this绑定 componentWillMount() render( ...
- 使用 jest 测试 react component 的配置,踩坑。
首先安装依赖 npm i jest -g npm i jest babel-jest identity-obj-proxy enzyme enzyme-adapter-react-15.4 react ...
- 转载 React.createClass 对决 extends React.Component
先给出结论,这其实是殊途同归的两种方式.过去我们一般都会使用 React.createClass 方法来创建组件,但基于 ES6 的小小语法糖,我们还可以通过 extends React.Compon ...
随机推荐
- WebService 布置简单的计算器
实验环境:myeclipse2015 具体的过程如下: 首先建立一个web service project 命名就为WebServiceProject,同时选择web service framewor ...
- Spring中的InitializingBean接口
InitializingBean接口为bean提供了初始化方法的方式,它只有afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候会执行该方法. 测试程序如下: impo ...
- 【codeforces 749D】Leaving Auction
[题目链接]:http://codeforces.com/problemset/problem/749/D [题意] 有n个人在竞价; 按照时间的顺序给出n次竞价(可能有一些人没有参加竞价); 每次竞 ...
- Android在程序中浏览网页
本文是自己学习所做笔记,欢迎转载.但请注明出处:http://blog.csdn.net/jesson20121020 有时须要在程序中浏览一些网页.当然了能够通过调用系统的浏览器来打开浏览.可是大多 ...
- ACM这一路
出自自己内心的声音. 大学已经读了一年,自己也老了一岁. 从開始的什么都不懂,到如今的懂了也不想多说什么,说多了也是累.在大学其中唯一还在执着的是ACM.这个也是我唯一能执着的东西,由 ...
- m_Orchestrate learning system---九、在无法保证是否有图片的情况下,如何保证页面格式
m_Orchestrate learning system---九.在无法保证是否有图片的情况下,如何保证页面格式 一.总结 一句话总结:都配上默认缩略图就可以解决了 1.如何获取页面get方式传过来 ...
- kafka 0.11 spark 2.11 streaming例子
""" Counts words in UTF8 encoded, '\n' delimited text received from the network every ...
- 文本编辑工具 Vim
Vim是Vi的升级版 vi和Vim的区别在于vi不会显示颜色,Vim会显示颜色 1.如果没有,可以安装 #yum install -y vim-enhanced
- Java8内置的四大核心函数式接口
package java_8; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import j ...
- 前端换行显示,后端返回<br>