2019独角兽企业重金招聘Python工程师标准>>>

上一节中<App/>组件传递状态temperatures给children -- <TemperatureInput />,如果组建是一个tree, 那么属性的传递则会非常繁琐。redux使用Provider给子组件提供store, connect将子组件和store联系起来,mobx使用Provider和inject注入

import ReactDOM from 'react-dom'
// import registerServiceWorker from './registerServiceWorker';
import React from 'react'
import { action, computed, observable } from 'mobx'
import { Provider, observer, inject } from 'mobx-react'
import DevTools from 'mobx-react-devtools'
const APPID = '415a88f2b45f08c3e561b058772ec6c3'class Temperature {id = Math.random()@observable unit = 'C'@observable temperatureCelsius = 25@observable location = 'Amsterdam, NL'@observable loading = trueconstructor (location) {this.location = locationthis.fetch()}@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'default:return this.temperatureCelsius + '°C'}}@action fetch () {window.fetch(`http://api.openweathermap.org/data/2.5/weather?appid=${APPID}&q=${this.location}`).then(res => res.json()).then(action(json => {this.temperatureCelsius = json.main.temp - 273.15this.loading = false}))}@action setUnit (newUnit) {this.unit = newUnit}@action setCelsius (degrees) {this.temperatureCelsius = degrees}@action('update temperature and unit')setTemperatureAndUnit (degrees, unit) {this.setUnit(unit)this.setCelsius(degrees)}@action inc() {this.setCelsius(this.temperatureCelsius + 1)}
}@inject('temperatures')
@observer
class TemperatureInput extends React.Component {@observable input = ''render () {return (<li>Destination<input value={this.input}onChange={this.onChange}/><button onClick={this.onSubmit}>Add</button></li>)}@action onChange = e => {this.input = e.target.value}@action onSubmit = () => {this.props.temperatures.push(new Temperature(this.input))this.input = ''}
}
@observer
class TView extends React.Component {render () {const t = this.props.temperaturereturn (<li key={t.id} onClick={() => this.onTemperatureClick()}>{t.location}: {t.loading ? 'loading...' : t.temperature}</li>)}@action onTemperatureClick = () => {this.props.temperature.inc()}
}
const App = inject('temperatures')(observer(({ temperatures }) => (<ul><TemperatureInput />{temperatures.map(t =><TView key={t.id} temperature={t} />)}<DevTools/></ul>)))const temps = observable([])ReactDOM.render(<Provider temperatures={temps}><App /></Provider>,document.getElementById('root')
)

转载于:https://my.oschina.net/u/2510955/blog/1833326

Mobx入门之三:Provider inject相关推荐

  1. python编程入门与案例详解课后题答案-Python入门之三角函数sin()函数实例详解

    描述 sin()返回的x弧度的正弦值. 语法 以下是sin()方法的语法: importmath math.sin(x) 注意:sin()是不能直接访问的,需要导入math模块,然后通过math静态对 ...

  2. python编程入门与案例详解pdf-Python入门之三角函数sin()函数实例详解

    描述 sin()返回的x弧度的正弦值. 语法 以下是sin()方法的语法: importmath math.sin(x) 注意:sin()是不能直接访问的,需要导入math模块,然后通过math静态对 ...

  3. python教程三角函数_Python入门之三角函数tan()函数实例详解

    描述 tan() 返回x弧度的正弦值. 语法 以下是 tan() 方法的语法: import math math.tan(x) 注意:tan()是不能直接访问的,需要导入 math 模块,然后通过 m ...

  4. (转载)VS2010/MFC编程入门之三十六(工具栏:工具栏资源及CToolBar类)

    上一节中鸡啄米讲了菜单及CMenu类的使用,这一节讲与菜单有密切联系的工具栏. 工具栏简介 工具栏一般位于主框架窗口的上部,菜单栏的下方,由一些带图片的按钮组成.当用户用鼠标单击工具栏上某个按钮时,程 ...

  5. sae mysql 同步本地_MYSQL入门之三_将本地MySQL数据导入SAE数据库_MySQL

    bitsCN.com MYSQL入门之三_将本地MySQL数据导入SAE数据库 1. MySQL字符集 MySQL的默认字符集是latin1,将本地MySQL库导出成sql,再导入到SAE的MySQL ...

  6. python画tan_Python入门之三角函数tan()函数实例详解

    描述 tan() 返回x弧度的正弦值. 语法 以下是 tan() 方法的语法: import math math.tan(x) 注意:tan()是不能直接访问的,需要导入 math 模块,然后通过 m ...

  7. Pandas高级数据分析快速入门之三——数据挖掘与统计分析篇

    Pandas高级数据分析快速入门之一--Python开发环境篇 Pandas高级数据分析快速入门之二--基础篇 Pandas高级数据分析快速入门之三--数据挖掘与统计分析篇 Pandas高级数据分析快 ...

  8. VS2013/MFC编程入门之三十四(工具栏:工具栏资源及CToolBar类)

    上一节中讲了菜单及CMenu类的使用,这一节讲与菜单有密切联系的工具栏. 工具栏简介 工具栏一般位于主框架窗口的上部,菜单栏的下方,由一些带图片的按钮组成.当用户用鼠标单击工具栏上某个按钮时,程序会执 ...

  9. python中反三角函数用法_Python入门之三角函数atan2()函数详解

    python 的 Python入门之三角函数atan2()函数详解 描述 atan2() 返回给定的 X 及 Y 坐标值的反正切值. 语法 以下是 atan2() 方法的语法: import math ...

最新文章

  1. 好玩的东西,测试一下
  2. DSML:深度子空间相互学习模型(用于癌症亚型预测)
  3. 民政部部长李纪恒:适龄人口生育意愿偏低,总和生育率破警戒线
  4. JavaScript进阶1-学习笔记
  5. r语言 断轴 画图_R语言作图——Density plot
  6. list取值_Redis中List及quicklist实现-2
  7. 代数学笔记11: 分圆域,分圆多项式,求解17次方程
  8. 记一个有趣的Java OOM!
  9. 终于有一次,排名没有进步
  10. 网页设计图片向上浮动_CSS实现图片向上浮动
  11. 微信小程序--行星轨迹
  12. 扁平化设计与思维导图
  13. xshell6使用技巧_Xshell6的正确打开方式
  14. 机电一体化c语言程序设计,高职机电一体化C语言程序设计研究
  15. 第一次尝试使Windows Live Writer发布日志
  16. 解析肖特基二极管的使用事项
  17. Dropwizard框架新手学习教程(详细版)
  18. 【Ngnix】Ngnix应用
  19. 中国直接针对消费者的疾病风险和健康DNA测试行业市场供需与战略研究报告
  20. python筛选股票_GitHub - sundroid/stock_pick: 这时一个通过设定选股条件来帮我们筛选股票的python程序...

热门文章

  1. AWS副总裁Swami:现在是人工智能和机器学习的黄金时代
  2. 火爆的图机器学习,2020年将有哪些研究趋势?
  3. 科普篇:贝叶斯网络中的置信度传播
  4. 在医疗领域你知道哪些人工智能黑科技?
  5. 2019年汽车AI计算技术及市场趋势
  6. 基于多特征地图和深度学习的实时交通场景分割
  7. CIO成功转型为战略性业务领导者的五个要领
  8. BAT面试题9:谈谈判别式模型和生成式模型?
  9. 寒门出贵子,传递正能量
  10. 心得丨学习人工智能AI需要哪些最基础的知识?