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/ ...
随机推荐
- java 多线程系列基础篇(九)之interrupt()和线程终止方式
1. interrupt()说明 在介绍终止线程的方式之前,有必要先对interrupt()进行了解.关于interrupt(),java的djk文档描述如下:http://docs.oracle.c ...
- 10-08C#基础--进制转换
(一).数制 计算机中采用的是二进制,因为二进制具有运算简单,易实现且可靠,为逻辑设计提供了有利的途径.节省设备等优点,为了便于描述,又常用八.十六进制作为二进制的缩写.一般计数都采用进位计数,其特点 ...
- linux中安装sqlmap
wget https://codeload.github.com/sqlmapproject/sqlmap/legacy.tar.gz/master //下载sqlmap tar zxvf maste ...
- Python之tuple的创建以及使用
tuple又是一种有序列表,它与list只有两种不同:1.创建后不能更改.2.创建时得用() 在使用tuple的时候我们需要进行那个添加一个“,” 如果不加的话就是一个整数. 但是tuple作为一个不 ...
- python中not的用法
python中的not具体表示是什么: 在python中not是逻辑判断词,用于布尔型True和False,not True为False,not False为True,以下是几个常用的not的用法: ...
- 【转】Xcode 清理存储空间
移除 Xcode 运行安装 APP 产生的缓存文件(DerivedData) 只要重新运行Xcode就一定会重新生成,而且会随着运行程序的增多,占用空间会越来越大.删除后在重新运行程序可能会稍微慢一点 ...
- 2-4 zookeeper配置文件介绍,运行zk
心跳机制就是超过一定的时间之后,那么这个从节点就会被抛弃. zookeeper需要存储的数据,比如说事务文件等等,它都会存到这个dataDir目录下. 如果是伪分布式的集群环境,那么它的端口肯定是要变 ...
- 关于PDF的一些书籍
PDF Explained: The ISO Standard for Document Exchange https://ssearch.oreilly.com/?q=PDF+
- Linux tee命令
一.简介 tee以标准输入作为输入,标准输出和文件作为输出. 二.语法 Usage: tee [OPTION]... [FILE]... Copy standard input to each F ...
- string为什么是final?源码分析
http://blog.csdn.net/zhangjg_blog/article/details/18319521