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. c 指针改变数字

    之前已经有了: gcc -c day4.c -Wall gcc -o day4.exe day4.o 所以才会有以下结果

  2. LeetCode 面试题 02.06. 回文链表

    题目链接:https://leetcode-cn.com/problems/palindrome-linked-list-lcci/ 编写一个函数,检查输入的链表是否是回文的. 示例 1: 输入: 1 ...

  3. [Python]scatter_matrix报错 module 'pandas' has no attribute 'scatter_matrix'

    运行pandas.scatter_matrix()散点图函数时报错, 原因是该函数在新版本用法发生了变化: pandas.plotting.scatter_matrix 完整用法:pd.plottin ...

  4. 容错保护机制:Spring Cloud Hystrix

    最近在学习Spring Cloud的知识,现将容错保护机制Spring Cloud Hystrix 的相关知识笔记整理如下.[采用 oneNote格式排版]

  5. Markdown数学公式如何打出回归符号

    来源:https://blog.csdn.net/garfielder007/article/details/51646604 函数.符号及特殊字符 语法 效果 语法 效果 语法 效果 \bar{x} ...

  6. final 和 static之间的区别和联系

    关键字final和关键字static两者的含义并不相似,但是笔者常常使用一段时间后就会忘记它们之间的区别,因为它俩总是相伴着出现.当只出现其中一个时,就对其代表的含义不甚清晰了.故而特地将相关知识点记 ...

  7. Java连载74-字符串常用方法、正则表达式简介

    一.字符串相关的常用方法简介​ package com.bjpowernode.java_learning; ​ public class D74_1_StringCommonMethod { pub ...

  8. C# WPF 表单更改提示

    微信公众号:Dotnet9,网站:Dotnet9,问题或建议,请网站留言: 如果您觉得Dotnet9对您有帮助,欢迎赞赏 C# WPF 表单更改提示 内容目录 实现效果 业务场景 编码实现 本文参考 ...

  9. C# compare different Encoding pattern between UTF8 and UTF32 based on Md5

    using System; using System.Text; using System.IO; using System.Security.Cryptography; static void Ma ...

  10. HDU1408 - 盐水的故事

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1408 解题思路:主要考虑最后一滴可能不满足D毫升,但仍算1秒.另外还要注意浮点数的比较. #inclu ...