1. const scaleNames = {
  2. c: 'Celsius',
  3. f: 'Fahrenheit'
  4. };
  5.  
  6. function toCelsius(fahrenheit) {
  7. return (fahrenheit - ) * / ;
  8. }
  9.  
  10. function toFahrenheit(celsius) {
  11. return (celsius * / ) + ;
  12. }
  13.  
  14. function tryConvert(temperature, convert) {
  15. const input = parseFloat(temperature);
  16. if (Number.isNaN(input)) {
  17. return '';
  18. }
  19. const output = convert(input);
  20. const rounded = Math.round(output * ) / ;
  21. return rounded.toString();
  22. }
  23.  
  24. function BoilingVerdict(props) {
  25. if (props.celsius >= ) {
  26. return <p>The water would boil.</p>;
  27. }
  28. return <p>The water would not boil.</p>;
  29. }
  30.  
  31. class TemperatureInput extends React.Component {
  32. constructor(props) {
  33. super(props);
  34. this.handleChange = this.handleChange.bind(this);
  35. }
  36.  
  37. handleChange(e) {
  38. this.props.onTemperatureChange(e.target.value);
  39. }
  40.  
  41. render() {
  42. const temperature = this.props.temperature;
  43. const scale = this.props.scale;
  44. return (
  45. <fieldset>
  46. <legend>Enter temperature in {scaleNames[scale]}:</legend>
  47. <input
  48. value={temperature}
  49. onChange={this.handleChange} />
  50. </fieldset>
  51. );
  52. }
  53. }
  54. class Calculator extends React.Component {
  55. constructor(props) {
  56. super(props);
  57. this.handleCelsiusChange = this.handleCelsiusChange.bind(this);
  58. this.handleFahrenheitChange = this.handleFahrenheitChange.bind(this);
  59. this.state = {temperature: '', scale: 'c'};
  60. }
  61.  
  62. handleCelsiusChange(temperature) {
  63. this.setState({scale: 'c', temperature});
  64. }
  65.  
  66. handleFahrenheitChange(temperature) {
  67. this.setState({scale: 'f', temperature});
  68. }
  69.  
  70. render() {
  71. const scale = this.state.scale;
  72. const temperature = this.state.temperature;
  73. const celsius = scale === 'f' ? tryConvert(temperature, toCelsius) : temperature;
  74. const fahrenheit = scale === 'c' ? tryConvert(temperature, toFahrenheit) : temperature;
  75. return (
  76. <div>
  77. <TemperatureInput
  78. scale="c"
  79. temperature={celsius}
  80. onTemperatureChange={this.handleCelsiusChange} />
  81. <TemperatureInput
  82. scale="f"
  83. temperature={fahrenheit}
  84. onTemperatureChange={this.handleFahrenheitChange} />
  85. <BoilingVerdict
  86. celsius={parseFloat(celsius)} />
  87. </div>
  88. );
  89. }
  90. }
  91.  
  92. ReactDOM.render(
  93. <Calculator />,
  94. document.getElementById('root')
  95. );

react 中文文档案例七 (温度计)的更多相关文章

  1. react 中文文档案例三 (开关按钮)

    开关按钮制作   import React from 'react'; import ReactDOM from 'react-dom'; class Toggle extends React.Com ...

  2. react 中文文档案例六 (表单)

    class Reservation extends React.Component { constructor(props) { super(props); this.state = { isGoin ...

  3. react 中文文档案例五 (循环列表)

    function NumberList(props) { const numbers = props.numbers; const listItems = numbers.map((number) = ...

  4. react 中文文档案例四 (登陆登出按钮)

    import React from 'react'; import ReactDOM from 'react-dom'; class LoginControl extends React.Compon ...

  5. react 中文文档案例二 (头像时间)

    import React from 'react'; import ReactDOM from 'react-dom'; function formatDate(date) { return date ...

  6. react 中文文档案例一 (倒计时)

    1.函数试组件 import React from 'react'; import ReactDOM from 'react-dom'; function Clock(props){ return( ...

  7. phpspreadsheet 中文文档(七)技巧和诀窍

    2019年10月11日14:08:35 以下页面为您提供了一些使用广泛的PhpSpreadsheet食谱.请注意,这些文件没有提供有关特定PhpSpreadsheet API函数的完整文档,而只是一个 ...

  8. talib 中文文档(七):Overlap Studies Functions

    Overlap Studies Functions 重叠指标 BBANDS - Bollinger Bands 函数名:BBANDS 名称: 布林线指标 简介:其利用统计原理,求出股价的标准差及其信赖 ...

  9. phpspreadsheet 中文文档 粗翻版

    2019年10月11日09:32:33 官方使用文档  https://phpspreadsheet.readthedocs.io/en/stable/topics/accessing-cells/ ...

随机推荐

  1. 11-03SQLserver基础--子查询语句

    一.子查询--查询的嵌套(重点记忆) select MAX(age)from haha where bumen='销售部' --汇总-- select MAX(age)from haha where  ...

  2. C语言学习笔记--#和##操作符

    1. #运算符 (1)#运算符用于在预处理期将宏的参数转换为字符串 (2)#的转换作用是在预处理期完成的,因此只在宏定义中有效,即其他地方不能用#运算符 (3)用法:#define STRING(x) ...

  3. DAY16-Django之model

    Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...

  4. 3-3 zookeeper的作用体现

    zookeeper比较重要的一个模式:选举模式,这也是高可用的一个体现.公司的董事长.副董事长.董事会常理员以及老总和副总,他们并不会乘坐同一班飞机,而是会分为两班或者三班飞机一起去,也就是我们所谓的 ...

  5. 05 HTML字符串转换成jQuery对象、绑定数据到元素上

    1 要求 将一段 HTML脚本 封装成一个字符串,将这个字符串转换成一个jQuery对象:然后将这个jQuery对象添加到指定的元素中去 2 步骤 定义字符串 var str = '<div i ...

  6. GUI编程02

    1 编写一个导航栏 from tkinter import * root = Tk() root.title("测试") root.geometry("400x400+4 ...

  7. Excel VBA连接MySql 数据库获取数据

    编写Excel VBA工具,连接并操作Mysql 数据库. 系统环境: OS:Win7 64位 英文版 Office 2010 32位 英文版 1.VBA连接MySql前的准备 Tools---> ...

  8. activeMQ功能Demo

    1. 请阐述ActiveMQ的作用 2. 请描述ActiveMQ的工作原理 1. 解决服务之间耦合 2. 使用消息队列,增加系统并发处理量 3. 使用Java程序编写生产者发送10条“你好,activ ...

  9. ROS Learning-026 (提高篇-004 A Mobile Base-02) 控制移动平台 --- “分封制”

    ROS 提高篇 之 A Mobile Base-02 - 控制移动平台 - "分封制" 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ub ...

  10. Blender 软件 四视图布局的创建方法

    Blender 软件 四视图布局的创建方法 我使用的Blender版本:Blender V 2.77 Q: 为什么要创建一个:四视图布局场景? A: 原因1:四视图布局最适合模型建模.原因2:在 Bl ...