react 中文文档案例七 (温度计)

const scaleNames = {
    c: ‘Celsius‘,
    f: ‘Fahrenheit‘
};

function toCelsius(fahrenheit) {
    return (fahrenheit - 32) * 5 / 9;
}

function toFahrenheit(celsius) {
    return (celsius * 9 / 5) + 32;
}

function tryConvert(temperature, convert) {
    const input = parseFloat(temperature);
    if (Number.isNaN(input)) {
        return ‘‘;
    }
    const output = convert(input);
    const rounded = Math.round(output * 1000) / 1000;
    return rounded.toString();
}

function BoilingVerdict(props) {
    if (props.celsius >= 100) {
        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‘)
);

原文地址:https://www.cnblogs.com/Lolita-web/p/10351732.html

时间: 2024-08-05 22:16:27

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

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

import React from 'react'; import ReactDOM from 'react-dom'; class LoginControl extends React.Component { constructor(props) { super(props); this.handleLoginClick = this.handleLoginClick.bind(this); this.handleLogoutClick = this.handleLogoutClick.bin

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

function NumberList(props) { const numbers = props.numbers; const listItems = numbers.map((number) => <li key={number.toString()}> {number} </li> ); return ( <ul>{listItems}</ul> ); } const numbers = [1, 2, 3, 4, 5]; ReactDOM.re

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

class Reservation extends React.Component { constructor(props) { super(props); this.state = { isGoing: true, numberOfGuests: 2, value: '' }; this.handleInputChange = this.handleInputChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this);

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

Overlap Studies Functions 重叠指标 BBANDS - Bollinger Bands 函数名:BBANDS 名称: 布林线指标 简介:其利用统计原理,求出股价的标准差及其信赖区间,从而确定股价的波动范围及未来走势,利用波带显示股价的安全高低价位,因而也被称为布林带. 分析和应用: 百度百科 同花顺学院 upperband, middleband, lowerband = BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2,

React中setState同步更新策略

本文和大家分享的主要是React中setState同步更新相关内容,希望对大家学习React有所帮助. 为了提高性能React将setState设置为批次更新,即是异步操作函数,并不能以顺序控制流的方式设置某些事件,我们也不能依赖于 this.state 来计算未来状态.典型的譬如我们希望在从服务端抓取数据并且渲染到界面之后,再隐藏加载进度条或者外部加载提示: componentDidMount() { fetch('https://example.com') .then((res) => re

前端新技术中文文档

nodejs中文网,官网同步翻译http://nodejs.cn/api/ Webpack 中文手册(社区同步翻译)http://6.course.uprogrammer.cn/webpack2-doc-cn/index.html ECMAScript6 中文教程https://www.w3cschool.cn/ecmascript/http://uprogrammer.cn/es6tutorial/ TypeScript 中文文档(官网同步翻译)https://tslang.cn/docs/h

React中key的作用

对于React中key的作用,官方没有给出详细的解读,只提到在List中需要给key赋值来区分每一条记录,http://blog.csdn.net/code_for_free/article/details/50514259里讲解了key的使用场景, http://taobaofed.org/blog/2016/08/24/react-key/从三方面讲了List组件中key存在的原因和List之外使用key作为trick简化代码逻辑,本文以List中新增元素为例,从List子组件生命周期函数的

【Chromium中文文档】Profile架构(看看谷歌家的重构)

进程模型 转载请注明出处:https://ahangchen.gitbooks.io/chromium_doc_zh/content/zh//General_Architecture/Profile_Architecture.html 全书地址 Chromium中文文档 for https://www.chromium.org/developers/design-documents 持续更新ing,欢迎star gitbook地址:https://ahangchen.gitbooks.io/ch

在React中使用Redux

修复遗留问题 webpack.prod.config.js中缺少了对path库的引用,执行构建npm run build:prod的时候失败.在文件开始的地方引入node.js的path库就可以了. package.json里面定义了一个build:dev的脚本,这个脚本其实有点多余,不过有时候需要打包测试版本的文件,所以还是需要存在.主要有个问题是webpack.dev.config.js中output节点下错误定义了path的值为根目录'/',这在使用npm start命令启动运行时打包的时