import * as React from "react"
    // 接收参数
interface IProps{
    work?:string
}

interface State {  //名字可以随便起
    date: string; // 定义 state 的结构  
  }  
    
export default class Hello extends React.Component<IProps,State>{
    // 组件初始化构造器
    public constructor(props:any){
        super(props);
        this.state = {  
            date: '11', // 初始化 state  
          };  
        //   参数调用的第一种方法 这里要先这么写 下面clickHandler2函数才能直接调用
        this.clickHandler2=this.clickHandler2.bind(this)
    }

    //参数调用的第2种方法----这样写就可以直接调用
    public clickHandler=()=>{
        this.setState({date:"111"})
    }

    //   参数调用的第一种方法
    public clickHandler2(){
        this.setState({date:"11"})
    }

    public render(){
        return (
            <div>
                
                Hello: {this.props.title}
                <br/>
                Hello: {this.props.age}
                {this.props.work}
                <br/>
                <p>当前日期: {this.state.date}</p> 
        //点击调用方法       
        <button onClick={this.clickHandler}>更新日期</button>  
        <button onClick={this.clickHandler2}>更新日期</button>
            </div>
            
        )
    }

}

 

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。