mobx 是由 Mendix、Coinbase、Facebook 开源和众多个人赞助商所赞助的。

mobx和redux类似,也可以用来进行状态管理,并且更简单,更灵活。初次研究,先实现一个最简单的功能修改一个字符串值

官网:https://cn.mobx.js.org

1、安装

yarn add mobx-react --save
yarn add mobx --save

...

2、添加import

import { observer } from 'mobx-react';
import { observable, computed, action } from 'mobx';

3、修改UserRole代码如下

import React from 'react';
import { observer } from 'mobx-react';
import { decorate,observable, computed, action } from 'mobx';
import { Button,Input} from 'antd';
@observer
class UserRole extends React.Component {
constructor(props) {
super(props);
}
@observable roleName = "123";
handleTestClick = e => {
this.roleName="234";
console.log(this.roleName);
};
render() {
return (
<div>角色管理
<div>{this.roleName}</div>
<Button type="primary" style={{marginTop:10,width:300}} onClick={this.handleTestClick}>测试</Button>
</div>
);
}
}
export default UserRole;
 

4、点击测试,可以看到不用从新对state赋值,即可自动渲染

5、如果不使用mobx,须使用setState才能实现类似效果,比较麻烦。代码如下

import React from 'react';
import { observer} from 'mobx-react';
import { decorate,observable, computed, action} from 'mobx';
import { Button,Input} from 'antd';
class UserRole extends React.Component { constructor(props) {
super(props);
this.state={
roleName:'test'
}
};
state:{
roleName
};
handleTestClick = e => {
this.setState({
roleName:'234'
});
console.log(this.state.roleName);
};
render() {
return (
<div>角色管理
<div>{this.state.roleName}</div>
<Button type="primary" style={{marginTop:10,width:300}} onClick={this.handleTestClick}>测试</Button>
</div>
);
}
} export default UserRole;

6、测试使用@compute

import React from 'react';
import { observer} from 'mobx-react';
import { decorate,observable, computed, action} from 'mobx';
import { Button,Input} from 'antd';
@observer
class UserRole extends React.Component { constructor(props) {
super(props);
};
@observable
roleName = "123";
@computed get roleNameInfo()
{
return "roleName"+this.roleName;
}
handleTestClick = e => {
this.roleName="234";
console.log(this.roleName);
};
render() {
return (
<div>角色管理
<div>{this.roleNameInfo}</div>
<div>{this.roleName}</div>
<Button type="primary" style={{marginTop:10,width:300}} onClick={this.handleTestClick}>测试</Button>
</div>
);
}
} export default UserRole;

也会自动Render

7、使用严格模式

import {observable, action, useStrict} from 'mobx';
useStrict(true);

在新版本中已经不能使用,估计是容易误解吧,代码如下

import { observable, action,configure } from 'mobx';
configure({enforceActions:true});

开启严格模式后

this.roleName="234";
这种操作会报错,必须放在@action 修饰的函数中才可以。mobx 4.x中变化

https://github.com/mobxjs/mobx/wiki/Migrating-from-mobx-3-to-mobx-4#things-that-just-have-moved

 
8、重构项目目录如下

9、可以两个页面组件共享一个State。

User.jsx中增加代码

import userRoleState from '../userrole/stores/UserRoleState';
render中添加
<div>{userRoleState.secend}</div>
事件中增加
userRoleState.add();
 

两个值会同步改变。

AntDesign(React)学习-11 使用mobx的更多相关文章

  1. AntDesign(React)学习-1 创建环境

    目录: AntDesign(React)学习-15 组件定义.connect.interface AntDesign(React)学习-14 使用UMI提供的antd模板 AntDesign(Reac ...

  2. AntDesign(React)学习-5 路由及使用Layout布局

    前言:学习目标实现点击登录按钮,直接进入后台布局页面,类似下面antd官网文档展示效果 ant.design访问 https://ant-design.gitee.io/components/menu ...

  3. AntDesign(React)学习-2 第一个页面

    1.前面创建了第一个项目jgdemo,结构如下,使用TypeScript. 2.yarn start启动项目 3.点击GettingStarted是umi的官方网站 https://umijs.org ...

  4. AntDesign(React)学习-14 使用UMI提供的antd模板

    1.UMI提供了可视化antd模板,可以直接添加到项目中修改用 比如将个人中心添加到项目中 2.选择个人中心,确定 3.成功 4.打开项目 5.Route文件也自动添加 根路由有exact:true后 ...

  5. AntDesign(React)学习-12 使用Table

    AntDesign(Vue)版的Table中使用图片https://www.cnblogs.com/zhaogaojian/p/11119762.html 之前在使用VUE版Table时,使用大图片时 ...

  6. AntDesign(React)学习-9 Dva model reducer实践

    今天肺炎增长数字依然吓人,感觉穿越到丧失片里了. 本节开始学习dva model使用,官网的讲解太文档化,对新手实践不太友好,自己简化了一个最简单的演示代码. 1.在src,models文件夹下创建u ...

  7. AntDesign(React)学习-8 Menu使用 切换框架页内容页面

    本节实现一个点击左侧menu在右侧content切换页面效果,原始代码请从UMI学习-6开始看 1.在pages下添加两个组件,User,UserRole import React from 'rea ...

  8. AntDesign(React)学习-4 登录页面提交数据简单实现

    github代码:https://github.com/zhaogaojian/jgdemo 全国肺炎,过节期间没地方去在家学习antd. 一.感觉antd pro项目太庞大了,可以学习下结构和代码风 ...

  9. AntDesign(React)学习-3 React基础

    前面项目已经建起来了,但是没有React基础怎么办,从头学习,这个项目使用的是基于React16.X版本的几种技术集成,那么我们就从网上找一些相关的资料进行研究,我的习惯是用到哪学到哪. 一.先看一些 ...

随机推荐

  1. MongoDB initial sync过程

    initial sync过程大致如下: (1)T1时间,从Primary同步所有数据库的数据,但不包括local的数据,复制时Mongo会扫描每个源数据库中的每个集合,并将所有数据插入对应的集合.通过 ...

  2. vc6 保存文件卡住

    解决办法:删除工程文件中的三个文件,分别是:*.ncb  * .opt   * .plg引用链接:https://blog.csdn.net/lvxianlong123/article/details ...

  3. Java中List的父类与子类如何转换?

    目录 定义 要点: 子类转父类 父类转子类 定义 A是B的子类,A比B多几条属性 要点: A是B的子类,但List<A>不是List<B>的子类.所以想直接转换是不行的. 子类 ...

  4. [P5748] 集合划分计数 - 生成函数,NTT

    求 \(10^5\) 以内的所有贝尔数:将 \(n\) 个有标号的球划分为若干非空集合的方案数 Solution 非空集合的指数生成函数为 \(F(x)=e^x-1\) 枚举一共用多少个集合,答案就是 ...

  5. 数字孪生 VS 平行系统

    数字孪生和平行系统作为新兴技术,在解决当今人工智能邻域面临的信息量大,干扰信息不确定因素多,与人的参与沟通更加紧密,人机互动更加重视,为了使人们有更好的体验人工智能带来的便利,急需推动信息物理社会的高 ...

  6. WebGL_0003:正则表达式查找字符串

    1,查找字符串,中间是变化的 files/assets/.*?/1/ .*? 表示中间是人一个字符

  7. C#排序算法的实现---冒泡排序

    一.算法原理 冒泡排序算法的运作如下: 比较相邻的元素.如果第一个比第二个大,就交换他们两个. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应该会是最大的数. 针对 ...

  8. C#实例之简单聊天室(状态管理)

    前言        状态管理是在同一页或不同页的多个请求发生时,维护状态和页信息的过程.因为Web应用程序的通信协议使用了无状态的HTTP协议,所以当客户端请求页面时,ASP.NET服务器端都会重新生 ...

  9. TamperMonkey 使用指南以及脚本推荐

    写在前面 Chrome浏览器是最适合开发者使用的浏览器,不仅仅是因为Chrome对于Js的友好支持,更是由于Chrome支持丰富且功能强大的插件,扩展了浏览器的功能和使用体验. 在这些插件里面,相信你 ...

  10. 自适应阈值化操作:adaptiveThreshold()函数

    在图像阈值化操作中,更关注的是从二值化图像中,分离目标区域和背景区域,但是仅仅通过设定固定阈值很难达到理想的分割效果.而自适应阈值,则是根据像素的邻域块的像素值分布来确定该像素位置上的二值化阈值.这样 ...