[Mobx] Use MobX actions to change and guard state
This lesson explains how actions can be used to control and modify the state of your application. They help you to structure your code base and integrate well with the MobX React Devtools. Actions automatically create transactions, which group changes together.
const {observable, computed} = mobx;
const {observer} = mobxReact;
const {Component} = React;
const DevTools = mobxDevtools.default;
const t = new class Temperature {
@observable unit = "C";
@observable temperatureCelsius = 25;
@computed get temperatureKelvin() {
console.log("calculating Kelvin")
return this.temperatureCelsius * (9/5) + 32
}
@computed get temperatureFahrenheit() {
console.log("calculating Fahrenheit")
return this.temperatureCelsius + 273.15
}
@computed get temperature() {
console.log("calculating temperature")
switch(this.unit) {
case "K": return this.temperatureKelvin + "ºK"
case "F": return this.temperatureFahrenheit + "ºF"
case "C": return this.temperatureCelsius + "ºC"
}
}
}
const App = observer(({ temperature }) => (
<div>
{temperature.temperature}
<DevTools />
</div>
))
ReactDOM.render(
<App temperature={t} />,
document.getElementById("app")
)
We have @Observable and @Computed defined, once we change any @Observable value, we can get new value in the @Computed.
But currently, the way we changing the value is though directly object mutation, such as:
t.unit = "F"
Of course, it is not good enough, what we can do is using @action to change the value:
const {observable, computed, action, transaction, useStrict} = mobx;
const {observer} = mobxReact;
const {Component} = React;
const DevTools = mobxDevtools.default;
useStrict(true);
const t = new class Temperature {
@observable unit = "C";
@observable temperatureCelsius = 25;
@computed get temperatureKelvin() {
console.log("calculating Kelvin")
return this.temperatureCelsius * (9/5) + 32
}
@computed get temperatureFahrenheit() {
console.log("calculating Fahrenheit")
return this.temperatureCelsius + 273.15
}
@computed get temperature() {
console.log("calculating temperature")
switch(this.unit) {
case "K": return this.temperatureKelvin + "ºK"
case "F": return this.temperatureFahrenheit + "ºF"
case "C": return this.temperatureCelsius + "ºC"
}
}
@action setUnit(newUnit) {
this.unit = newUnit;
}
@action setCelsius(degrees) {
this.temperatureCelsius = degrees;
}
@action("update temperature and unit")
setTemperatureAndUnit(degrees, unit) {
this.setCelsius(degrees);
this.setUnit(unit);
}
}
const App = observer(({ temperature }) => (
<div>
{temperature.temperature}
<DevTools />
</div>
))
ReactDOM.render(
<App temperature={t} />,
document.getElementById("app")
)
Action can be anynomous action or named action:
@action setCelsius(degrees)
@action("update temperature and unit") // named
[Mobx] Use MobX actions to change and guard state的更多相关文章
- 【MobX】MobX 简单入门教程
一.MobX 介绍 首先看下官网介绍: MobX 是一个经过战火洗礼的库,它通过透明的函数响应式编程(transparently applying functional reactive progra ...
- [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 s ...
- FCC---Use CSS Animation to Change the Hover State of a Button---鼠标移过,背景色变色,用0.5s的动画制作
You can use CSS @keyframes to change the color of a button in its hover state. Here's an example of ...
- M1事后分析报告--We have power to change the origin state
M1事后分析报告 设计与实现 我们发的软件解决的问题?是否满足后面小组的要求?是否能够完全拟合前期目标? 答: 前期我们的软件完成量并不是特别让人满意,我们组在完成这些任务量之后,发现有很多地方是在做 ...
- 你需要Mobx还是Redux?
在过去一年,越来越多的项目继续或者开始使用React和Redux开发,这是目前前端业内很普遍的一种前端项目解决方案,但是随着开发项目越来越多,越来越多样化时,个人又有了不同的感受和想法.是不是因为已经 ...
- [Web] How to Test React and MobX with Jest
转载自: https://semaphoreci.com/community/tutorials/how-to-test-react-and-mobx-with-jest?utm_content=bu ...
- Redux/Mobx/Akita/Vuex对比 - 选择更适合低代码场景的状态管理方案
近期准备开发一个数据分析 SDK,定位是作为数据中台向外输出数据分析能力的载体,前端的功能表现类似低代码平台的各种拖拉拽.作为中台能力的载体,SDK 未来很大概率会需要支持多种视图层框架,比如Vue2 ...
- 十分钟介绍mobx与react
原文地址:https://mobxjs.github.io/mobx/getting-started.html 写在前面:本人英语水平有限,主要是写给自己看的,若有哪位同学看到了有问题的地方,请为我指 ...
- [Web 前端] mobx教程(三)-在React中使用Mobx
copy from : https://blog.csdn.net/smk108/article/details/85053903 Mobx提供了一个mobx-react包帮助开发者方便地在React ...
随机推荐
- DEDECMS教程:列表页缩略图随机调用
如果用过DEDECMS的朋友应该都知道,有些模板列表页面需要用到缩略图,调用内容中的缩略图可以使用系统自带的脚本调用第一张图片.但是,并不是我们所有的内容里都有图片,有时候第一张图片也不一定是适合尺寸 ...
- element-ui表格控件前端分页方法
<div id="app"> <el-table :data="tableData.slice((currentPage-1)*pageSize,cur ...
- 【Django】安装及配置
目录 MVC框架与MTV框架 Django的MTV模式 Django框架图示 安装及配置 创建一个Django项目 目录介绍 运行Django项目 启动Django报错 模版文件配置 静态文件配置 A ...
- QNX与Linux两家未来有望独霸车载电子操作系统
车载电子操作系统是汽车智能化的核心,能够有效分配车机的硬件资源,对车内各种任务功能进行协同管理,并控制各项任务优先级别.常见的车载电子操作系统有:QNX.Linux(Android,AaliOS).W ...
- Oracle Database Sample Schemas
本文在Creative Commons许可证下发布 最近在钻研Oracle 11gR2,写SQL缺乏Demo表,研究他家的官方资料时发现一块甲骨文已经给我们准备Sample Schemas.比如说SC ...
- QQ,新浪,SNS等公众平台的登录及api操作
QQ的写法地址:http://www.oschina.net/code/snippet_930167_19888 Sina的写法地址:http://www.oschina.net/code/snipp ...
- 【例题 7-11 UVA - 12325】Zombie's Treasure Chest
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 1.N/s1 < 1e6 枚举1的个数 2.N/s2<1e6 枚举2的个数 3.s1和s2的值较小 假设买了s2个1和s ...
- github连接报"ssh: connect to host github.com port 22: Connection timed out"错误
1. 异常 在连接github时,执行"ssh -T git@github.com" 命令时,出现 ssh: connect to host github.com port 22: ...
- jvm vmthread
http://www.360doc.com/content/15/0615/16/15758456_478311946.shtml http://www.51testing.com/html/95/1 ...
- 洛谷 P1360 [USACO07MAR]黄金阵容均衡Gold Balanced L…
P1360 [USACO07MAR]黄金阵容均衡Gold Balanced L… 题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many simi ...