[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 ...
随机推荐
- LG Gram 2018 z980 白
因为今年8代处理器i5的双核变成了四核,感觉是个换电脑的好时机,本来打算买macbook,但是6月的发布会并没有发布,于是开始寻找一些比较有特点的笔记本电脑. 了解了这样一款笔记本 LG GRAM 1 ...
- 海思平台服务器版软件V15.2产品发布
深度操作系统海思平台服务器版软件是武汉深之度科技有限公司发布的针对华为海思平台的TaiShan系列服务器发布的企业级服务器操作系统软件产品,主要面向企业级服务器应用场景,为用户在国产化平台上提供更具可 ...
- Centos6.5 安装lnmp环境
最近项目要配置在nginx上,所以搜索了下具体nginx的安装,看到这篇文章简洁明了而且测试成功就借用了,作品出处:http://www.cnblogs.com/xiaoit/p/3991037.ht ...
- linux 查看tomcat 实时日志
进入tomcat下logs文件夹下,若没有Catalina.out文件先去启动服务在回到logs文件夹输入 tail -f catalina.out ,可看到实时日志
- CSUOJ 1637 Yet Satisfiability Again!
1637: Yet Satisfiability Again! Time Limit: 5 Sec Memory Limit: 128 MB Description Alice recently s ...
- spring 使用c3po连接池
1 数据源:能够简单理解为数据的来源. 2 连接池:是缓存一定数量的数据库连接,当程序须要数据库连接的时候,直接在连接池中获取空暇的连接,使用完再放回连接池中,此连接又变成空暇状态,等待下一次连接. ...
- region实现大纲效果
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- PDF Adobe Acrobat 9 简体中文专业版(打印店内部的软件)(你懂的!)
福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号: 大数据躺过的坑 Java从入门到架构师 人工智能躺过的坑 Java全栈大联盟 ...
- Kinect 开发 —— Kinect studio
This tool can record all the data coming into an application from a Kinect unit. You can then view, ...
- OpenCV —— 视频播放控制
创建滚动条,实现滚动条随视频播放移动. #include "cv.h" #include "highgui.h" CvCapture* capture=NULL ...