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. 搭建Linux(Ubuntu)系统下的Differential Datalog运行环境

    DDlog is a bottom-up, incremental, in-memory, typed Datalog engine. It is well suited for writing pr ...

  2. .NET CLI简单使用

    官方文档https://docs.microsoft.com/zh-cn/dotnet/core/tools/?tabs=netcore2x 创建新项目 查看能创建什么类型的项目 dotnet new ...

  3. mybatis + oracle 自增 结合navicate

    1.navicate建表 //T_USER表建立序列T_USER_SQCREATE SEQUENCE T_USER_SQ INCREMENT BY NOMAXVALUE NOCYCLE CACHE ; ...

  4. Android 调试桥 (adb) 是 命令行命令--官方拷贝过来的,留作自己查看

    Android 调试桥 (adb) 是一种功能多样的命令行工具,可让您与设备进行通信.adb 命令便于执行各种设备操作(例如安装和调试应用),并提供对 Unix shell(可用来在设备上运行各种命令 ...

  5. 首次使用Lambda表达式-sunziren

    需要将List<Apple> list = new ArrayList<Apple>(); 按照Apple对象中的price属性从大到小排序. 第一个念头闪过的是冒泡排序,转念 ...

  6. [USACO19FEB]Painting the Barn G

    题意 \(n\)个矩阵\((0\le x_1,y_1,x_2,y_2\le 200)\),可交,可以再放最多两个矩阵(这两个矩阵彼此不交),使得恰好被覆盖\(k\)次的位置最大.\(n,k\le 10 ...

  7. QQ常用表情

    以下表情均为QQ官方表情原图,版权归QQ所有,禁止用于商业用途. ![3nEdY9.png](https://s2.ax1x.com/2020/02/21/3nEdY9.png) ![3nEaFJ.p ...

  8. [TJOI2015] 概率论 - Catalan数

    一棵随机生成的 \(n\) 个结点的有根二叉树(所有互相不同构的形态等概率出现)的叶子节点数的期望.\(n \leq 10^9\) Solution \(n\) 个点的二叉树个数即 Catalan 数 ...

  9. C#MVC用ZXing.Net生成二维码/条形码

    开篇:zxing.net是.net平台下编解条形码和二维码的工具. 首先创建新项目 选择MVC模板  添加一个控制器  在项目引用中的引用ZXing 进行联网下载 控制器需要引用 后台控制器   pu ...

  10. HTML5 canvas绘图基础(电子名片生成器源码)

    创建canvas <canvas id="myCanvas" class="canvas"> 您的浏览器不支持canvas </canvas& ...