[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 ...
随机推荐
- debian 9 安装后的配置,debian 9 开发环境。
注意:以下命令用sudo或者以root用户进行 一.Xterm(在安装KDE桌面情况下)的配置(可以黏贴,复制): 首先在根目录下编辑文件.Xresources(没有可以创建) root@debian ...
- HDU 4418 高斯消元法求概率DP
把两种状态化成2*n-2的一条线上的一种状态即可.很容易想到. 高斯列主元法,不知为什么WA.要上课了,不玩了...逃了一次课呢.. #include <iostream> #includ ...
- Revolution Platform
Revolution Platform 黑暗的极权统治现实 异类的处境 独孤的存在 觉者的形成 信仰的确立 信仰的产物 完整的思想理论 反抗与信仰的一致 反抗的超理性的智慧论 反抗的纯理性的方法论 反 ...
- Java类载入器
1. 系统载入器简单介绍 Java虚拟机中能够安装多个类载入器,系统默认三个主要类载入器(BootStrap.ExtClassLoader.AppClassLoader).每一个类载入器负责载入特 ...
- Android技术归档
各位小伙伴们.以后小巫的一些开源码都会上传到github中,所以欢迎大家Follow https://github.com/devilWwj 基于眼下我基本的技术领域在Android上,以后关于And ...
- 89.[NodeJS] Express 模板传值对象app.locals、res.locals
转自:https://blog.csdn.net/Elliott_Yoho/article/details/53537437 locals是Express应用中 Application(app)对象和 ...
- MySQL内置函数uuid和uuid_short
MySQL的uuid这个函数.简要介绍一下. 用法 简单看到,这个值,每次执行都是不同的. 生成规则 第1 2 3 段是与时间有关的. time_low.time_mid.time_high_and_ ...
- Springboot如何利用http请求控制器
写好了接口,现在想用postman测试一下这个接口是否正确,想请问怎么传入请求参数?先谢谢了! Springboot如何利用http请求控制器 >> java这个答案描述的挺清楚的:htt ...
- 快速傅里叶变换(Fast-Fourier Transform,FFT)
数学定义: (详细参考:https://www.baidu.com/link?url=oYAuG2o-pia_U3DlF5n_MJZyE5YKfaVRUHTTDbM1FwM_kDTjGCxKpw_Pb ...
- redis 篇 - 键 and string
redis 进入控制台 redis-cil 需要输入密码的时候可以是用 -a redis-cil -a abcd1234 redis 数据类型 string hash list set zset( 有 ...