zoukankan      html  css  js  c++  java
  • React十进制和二进制转换的实现和分析

    【描述】

    模仿官方文档的摄氏度和华氏度的转换,实现十进制和二进制的互换。

    【实现】

    import React from 'react';
    import ReactDOM from 'react-dom';
    
    function Convert(value,setWhat){
        if(value===''){return ''}
        if(setWhat==='setDec'){
            return parseInt(value,2);
        }else{
            return parseInt(value).toString(2);
        }
    }
    
    class InputBox extends React.Component{
        constructor(props){
            super(props);
            this.handleChange=this.handleChange.bind(this);
        }
        handleChange(e){
            //数据流向:子->父。onChange事件接受父组件的回调函数,实现把event.target.value传到父组件
            this.props.callBack(e.target.value); 
        }
        render(){
            return(
                <div>
                    <p>Input: {this.props.title}</p>
                    <input value={this.props.selfValue} onChange={this.handleChange} />
                </div>
            )
        }
    }
    
    class Main extends React.Component{
        constructor(props){
            super(props);
            this.callBack_changeDec=this.callBack_changeDec.bind(this);
            this.callBack_changeBin=this.callBack_changeBin.bind(this);
            this.state={
                wanted:'wantDec',
                result:'',
            }
        }
        callBack_changeDec(inputValue){
            this.setState({result:inputValue});
            this.setState({wanted:'wantBin'});
        }
        callBack_changeBin(inputValue){
            this.setState({result:inputValue});
            this.setState({wanted:'wantDec'});
        }
        
        render(){
            var result_titleIsDec=this.state.wanted==='wantBin'?this.state.result:Convert(this.state.result,'setDec');
            var result_titleIsBin=this.state.wanted==='wantBin'?Convert(this.state.result,'setBin'):this.state.result;
            return(
                <div>
                    <InputBox title='Dec' callBack={this.callBack_changeDec} selfValue={result_titleIsDec}/>
                    <InputBox title='Bin' callBack={this.callBack_changeBin} selfValue={result_titleIsBin}/>
                </div>
            )
        }
    }
    ReactDOM.render(<Main />, document.getElementById('root'));

    【解析】

  • 相关阅读:
    gitlab: git clone/pull / push: The project you were looking for could not be found
    转载: MySQL启动出错InnoDB: Check that you do not already have another mysqld process解决方法
    root用户删除文件,提示:Operation not permitted
    使用dockerfile打包新镜像
    kubernets创建Deployment
    代理全家福
    Spring事务传播详解
    [FFmpeg]Centos7 yum安装
    [Redis]存放字典
    [Docker]开放2375端口
  • 原文地址:https://www.cnblogs.com/remly/p/10293650.html
Copyright © 2011-2022 走看看