react 中文文档案例七 (温度计)
- const scaleNames = {
- c: 'Celsius',
- f: 'Fahrenheit'
- };
- function toCelsius(fahrenheit) {
- return (fahrenheit - ) * / ;
- }
- function toFahrenheit(celsius) {
- return (celsius * / ) + ;
- }
- function tryConvert(temperature, convert) {
- const input = parseFloat(temperature);
- if (Number.isNaN(input)) {
- return '';
- }
- const output = convert(input);
- const rounded = Math.round(output * ) / ;
- return rounded.toString();
- }
- function BoilingVerdict(props) {
- if (props.celsius >= ) {
- return <p>The water would boil.</p>;
- }
- return <p>The water would not boil.</p>;
- }
- class TemperatureInput extends React.Component {
- constructor(props) {
- super(props);
- this.handleChange = this.handleChange.bind(this);
- }
- handleChange(e) {
- this.props.onTemperatureChange(e.target.value);
- }
- render() {
- const temperature = this.props.temperature;
- const scale = this.props.scale;
- return (
- <fieldset>
- <legend>Enter temperature in {scaleNames[scale]}:</legend>
- <input
- value={temperature}
- onChange={this.handleChange} />
- </fieldset>
- );
- }
- }
- class Calculator extends React.Component {
- constructor(props) {
- super(props);
- this.handleCelsiusChange = this.handleCelsiusChange.bind(this);
- this.handleFahrenheitChange = this.handleFahrenheitChange.bind(this);
- this.state = {temperature: '', scale: 'c'};
- }
- handleCelsiusChange(temperature) {
- this.setState({scale: 'c', temperature});
- }
- handleFahrenheitChange(temperature) {
- this.setState({scale: 'f', temperature});
- }
- render() {
- const scale = this.state.scale;
- const temperature = this.state.temperature;
- const celsius = scale === 'f' ? tryConvert(temperature, toCelsius) : temperature;
- const fahrenheit = scale === 'c' ? tryConvert(temperature, toFahrenheit) : temperature;
- return (
- <div>
- <TemperatureInput
- scale="c"
- temperature={celsius}
- onTemperatureChange={this.handleCelsiusChange} />
- <TemperatureInput
- scale="f"
- temperature={fahrenheit}
- onTemperatureChange={this.handleFahrenheitChange} />
- <BoilingVerdict
- celsius={parseFloat(celsius)} />
- </div>
- );
- }
- }
- ReactDOM.render(
- <Calculator />,
- document.getElementById('root')
- );
react 中文文档案例七 (温度计)的更多相关文章
- react 中文文档案例三 (开关按钮)
开关按钮制作 import React from 'react'; import ReactDOM from 'react-dom'; class Toggle extends React.Com ...
- react 中文文档案例六 (表单)
class Reservation extends React.Component { constructor(props) { super(props); this.state = { isGoin ...
- react 中文文档案例五 (循环列表)
function NumberList(props) { const numbers = props.numbers; const listItems = numbers.map((number) = ...
- react 中文文档案例四 (登陆登出按钮)
import React from 'react'; import ReactDOM from 'react-dom'; class LoginControl extends React.Compon ...
- react 中文文档案例二 (头像时间)
import React from 'react'; import ReactDOM from 'react-dom'; function formatDate(date) { return date ...
- react 中文文档案例一 (倒计时)
1.函数试组件 import React from 'react'; import ReactDOM from 'react-dom'; function Clock(props){ return( ...
- phpspreadsheet 中文文档(七)技巧和诀窍
2019年10月11日14:08:35 以下页面为您提供了一些使用广泛的PhpSpreadsheet食谱.请注意,这些文件没有提供有关特定PhpSpreadsheet API函数的完整文档,而只是一个 ...
- talib 中文文档(七):Overlap Studies Functions
Overlap Studies Functions 重叠指标 BBANDS - Bollinger Bands 函数名:BBANDS 名称: 布林线指标 简介:其利用统计原理,求出股价的标准差及其信赖 ...
- phpspreadsheet 中文文档 粗翻版
2019年10月11日09:32:33 官方使用文档 https://phpspreadsheet.readthedocs.io/en/stable/topics/accessing-cells/ ...
随机推荐
- 11-03SQLserver基础--子查询语句
一.子查询--查询的嵌套(重点记忆) select MAX(age)from haha where bumen='销售部' --汇总-- select MAX(age)from haha where ...
- C语言学习笔记--#和##操作符
1. #运算符 (1)#运算符用于在预处理期将宏的参数转换为字符串 (2)#的转换作用是在预处理期完成的,因此只在宏定义中有效,即其他地方不能用#运算符 (3)用法:#define STRING(x) ...
- DAY16-Django之model
Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...
- 3-3 zookeeper的作用体现
zookeeper比较重要的一个模式:选举模式,这也是高可用的一个体现.公司的董事长.副董事长.董事会常理员以及老总和副总,他们并不会乘坐同一班飞机,而是会分为两班或者三班飞机一起去,也就是我们所谓的 ...
- 05 HTML字符串转换成jQuery对象、绑定数据到元素上
1 要求 将一段 HTML脚本 封装成一个字符串,将这个字符串转换成一个jQuery对象:然后将这个jQuery对象添加到指定的元素中去 2 步骤 定义字符串 var str = '<div i ...
- GUI编程02
1 编写一个导航栏 from tkinter import * root = Tk() root.title("测试") root.geometry("400x400+4 ...
- Excel VBA连接MySql 数据库获取数据
编写Excel VBA工具,连接并操作Mysql 数据库. 系统环境: OS:Win7 64位 英文版 Office 2010 32位 英文版 1.VBA连接MySql前的准备 Tools---> ...
- activeMQ功能Demo
1. 请阐述ActiveMQ的作用 2. 请描述ActiveMQ的工作原理 1. 解决服务之间耦合 2. 使用消息队列,增加系统并发处理量 3. 使用Java程序编写生产者发送10条“你好,activ ...
- ROS Learning-026 (提高篇-004 A Mobile Base-02) 控制移动平台 --- “分封制”
ROS 提高篇 之 A Mobile Base-02 - 控制移动平台 - "分封制" 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ub ...
- Blender 软件 四视图布局的创建方法
Blender 软件 四视图布局的创建方法 我使用的Blender版本:Blender V 2.77 Q: 为什么要创建一个:四视图布局场景? A: 原因1:四视图布局最适合模型建模.原因2:在 Bl ...