[MobX] MobX fundamentals: deriving computed values and managing side effects with reactions
Derivations form the backbone of MobX and come in two flavors: computed values are values that can be derived from the state automatically. And reactions can be used to manage side effects, such as drawing the user interface. In this lesson you will learn how these concepts relate to each other and how they are optimized automatically by MobX.
const {observable, computed} = mobx;
const {observer} = mobxReact;
const {Component} = React;
const DevTools = mobxDevtools.default;
const t = new class Temperature {
@observable unit = "C";
@observable temperatureCelsius = ;
@computed get temperatureKelvin() {
console.log("calculating Kelvin")
return this.temperatureCelsius * (/) +
}
@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")
)
If 'unit' or 'temperateureCelsius' changed, it will automaticlly trigger the corresponding @computed function to run based on current state
[MobX] MobX fundamentals: deriving computed values and managing side effects with reactions的更多相关文章
- [React + Mobx] Mobx and React intro: syncing the UI with the app state using observable and observer
Applications are driven by state. Many things, like the user interface, should always be consistent ...
- Redux/Mobx/Akita/Vuex对比 - 选择更适合低代码场景的状态管理方案
近期准备开发一个数据分析 SDK,定位是作为数据中台向外输出数据分析能力的载体,前端的功能表现类似低代码平台的各种拖拉拽.作为中台能力的载体,SDK 未来很大概率会需要支持多种视图层框架,比如Vue2 ...
- 你需要Mobx还是Redux?
在过去一年,越来越多的项目继续或者开始使用React和Redux开发,这是目前前端业内很普遍的一种前端项目解决方案,但是随着开发项目越来越多,越来越多样化时,个人又有了不同的感受和想法.是不是因为已经 ...
- [Web 前端] mobx教程(二)-mobx主要概念
cp from : https://blog.csdn.net/smk108/article/details/84960159 通过<Mobx教程(一)-Mobx简介>我们简单理解了Mob ...
- 初见mobX
先看如下的代码 const {observable}= mobox; const {observer}=mobxReact; const {Component}=React; const appSta ...
- [Web 前端] MobX
1. 介绍 1.1. 原理 React的render是 状态 转化为树状结构的渲染组件的方法 而MobX提供了一种存储,更新 状态 的方法 React 和 MobX都在优化着软件开发中相同的问题. R ...
- react-native 中使用 mobx
1. 介绍 1.1. 原理 React的render是 状态 转化为树状结构的渲染组件的方法而MobX提供了一种存储,更新 状态 的方法React 和 MobX都在优化着软件开发中相同的问题.Reac ...
- redux和mobx比较(二)
Redux Redux 是 JavaScript 状态容器,提供可预测化的状态管理. 三大核心 在 Redux 中,最为核心的概念就是 action .reducer.store 以及 state,那 ...
- React + MobX 状态管理入门及实例
前言 现在最热门的前端框架,毫无疑问是React. React是一个状态机,由开始的初始状态,通过与用户的互动,导致状态变化,从而重新渲染UI. 对于小型应用,引入状态管理库是"奢侈的&qu ...
随机推荐
- java(面向对象 )
java面向对象的语言 对象:真实存在唯一的事物. 类:实际就是对某种类型事物的共性属性与行为的抽取. 面向对象的计算机语言核心思想: 找适合的对象做适合的事情. 如何找适合的对象: 1.sun已经定 ...
- vue 遇到的报错
[Vue warn]: Invalid default value for prop "dataParams": Props with type Object/Array must ...
- CMDB学习之五服务端api
服务端api 对发送来的数据进行处理,并返回结果,首先要创建一个Django项目 第一步,就是写URL路由在分支中写url api 主路由 from django.conf.urls import u ...
- 【习题 8-6 UVA - 1611】 Crane
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 想把数字i从位置j移动到位置i 可以这样. 假设mov(x,y)表示将(x..x+len/2-1)和(x+len/2..y)交换. ...
- HDU 5063 Operation the Sequence(暴力 数学)
题目链接:pid=5063" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=5063 Prob ...
- 程序猿果真有前端后端client吗
前端 后端 client DBA OP 程序猿有分这么细的吗? 入行时候有区别. 殊途同归 吾道一以贯之, 假设作为程序猿不能领悟一贯, 则永远不清楚.
- 8lession-基础类型转化
Python数据类型转换 有时候,我们需要对数据内置的类型进行转换,数据类型的转换,你只需要将数据类型作为函数名即可. 以下几个内置的函数可以执行数据类型之间的转换.这些函数返回一个新的对象,表示转换 ...
- JavaScript中的global对象,window对象以及document对象的区别和联系
JavaScript中的global对象,window对象以及document对象的区别和联系 一.概念区分:JavaScript中的global对象,window对象以及document对象 1.g ...
- 建堆是 O(n) 的时间复杂度证明。
建堆的复杂度先考虑满二叉树,和计算完全二叉树的建堆复杂度一样. 对满二叉树而言,第 \(i\) 层(根为第 \(0\) 层)有 \(2^i\) 个节点. 由于建堆过程自底向上,以交换作为主要操作,因此 ...
- 深入了解Linux远程桌面
本文转载于:http://www.linux521.com/2009/system/201004/11001.html 已发表在<网管员世界>2010年3月杂志 本 ...