zoukankan      html  css  js  c++  java
  • React基础知识备忘

    section-1

    //react组件 
    export class Halo extends React.Component{
        constructor(...args){
            super(...args); //初始化父类构造函数
            this.state={ //设置state
                text:""
            }
        }
        hello(ev){
           this.setState({ //修改state
               text:ev.target.value
           })
        }
        render(){
            return(
                  {/*只能一个父元素包裹*/} 
                  <div>
                      {/*事件大小写注意 onChange onClick*/} 
                      <input type="text" onChange= {this.hello.bind(this)}/>
                      <span>{this.state.text}</span>
                  </div>
            )
        }
    }
    

    section-2

    export class Halo extends React.Component{
        constructor(...args){
            super(...args);
            this.state={
                display:"block"
            }
        }
        toggle(){
           this.setState({
                display:this.state.display==="none" ? "block":"none"
           })
        }
        render(){
            return(
                  <div>
                      <input type="button" value="切换" onClick={this.toggle.bind(this)}/>
                      {/*class在JSX中需要改成className*/}
                      {/*行内样式需要加两个花括号*/}
                      <span className="content" style={{display:this.state.display}}>内容的显示和隐藏</span>
                  </div>
            )
        }
    }
    

    section-3

    export class Halo extends React.Component{
        constructor(...args){
            super(...args);
            this.state={
                h:0,
                m:0,
                s:0
            };
            setInterval(function(){
                this.update();
            }.bind(this),1000)
        }
        update(){
            let date=new Date();
            this.setState({
                h:date.getHours(),
                m:date.getMinutes(),
                s:date.getSeconds()
            })
        }
        componentDidMount(){
            this.update();
        }
        render(){
            return(
                  <div>
                      {this.state.h}:{this.state.m}:{this.state.s}
                  </div>
            )
        }
    }
    

    section-4

    /*react 生命周期*/
    
    componentWillMount()   创建前
    componentDidMount()    创建后
    
    componentWillUpdate()  更新前
    componentDidUpdate()   更新后
    
    componentWillUnMount() 销毁前
    
    componentWillReceiveProps(nextProps){
      // ??? 
    }
    
    shouldComponentUpdate(){
        //返回boolean值 默认每次状态更改时重新渲染
        //返回false componentWillUpdate(),render()和componentDidUpdate()将不会被调用
    }
    

    section-5

    //组件的另一种写法 无状态state组件
    //Es6 React.Component  Es5 React.createClass 其他两种定义方式
    let Item=function(props){
        return <li>{props.value}</li>
    };
    
    export class Halo extends React.Component{
        constructor(...args){
            super();
            this.state={
                arr:[1,2,3,4,5]
            }
        }
        addItem(){
            this.setState({
                arr:this.state.arr.concat([Math.random()])
            })
        }
        render(){
            let result=[],arr=this.state.arr;
            for(var i=0;i<arr.length;i++){
                {/*需要给每个Item增加unique key唯一标识*/}
                result.push(<Item key={i} value={arr[i]} />)
            }
            return(
              <div>
                  <input type="button" value="增加Item" onClick={this.addItem.bind(this)}/>
                  <ul>
                      {result}
                  </ul>
              </div>
            )
        }
    }
  • 相关阅读:
    生成.project、.classpath文件
    Ecelipse上添加Server
    通信安全验证
    通过jstack定位在线运行java系统故障_案例1
    自动代码复制工具
    在Visual Studio Express 2013中开发自定义控件
    通过java类文件识别JDK编译版本
    去掉java反编译(JD-GUI)生成的源文件中注释
    循环处理目录下文件框架
    java查找重复类/jar包/普通文件
  • 原文地址:https://www.cnblogs.com/leyi/p/7994792.html
Copyright © 2011-2022 走看看