码迷,mamicode.com
首页 > 其他好文 > 详细

React

时间:2020-01-20 20:39:13      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:obj   struct   license   world   length   hang   target   OLE   turn   

       <script type="text/babel">
           let colorArr = ["red","yellow","blue","orange","pink","green","gray"];
             class HelloWorld extends React.Component{
              constructor(props){
                super(props);
                this.state={
                  bgColor:"yellow"
                };
                this.toggleColor=this.toggleColor.bind(this);
                 this.changeColor=this.changeColor.bind(this);
              }
              
              
              toggleColor(){
                if(this.state.bgColor=="yellow"){
                  this.setState({bgColor:"red"});
                }else{
                   this.setState({bgColor:"yellow"});
                }
              }
              
              componentDidMount(){
                  let colorPos = 0;
                  setInterval(()=>{
                      this.setState({bgColor:colorArr[colorPos]});
                      if(colorArr.length-1>colorPos){
                          colorPos++;
                      }else{
                          colorPos=0;
                      }
                      
                  },1000)
              }
              changeColor(event){
                  this.setState({bgColor:event.target.value})
              }
              render(){
                console.log(this.props.name);
                //jsx
                let objStyle = {background:this.state.bgColor,fontSize:26};
                return (<div id="jieshao" style={objStyle} onClick={this.toggleColor}>
                         <h3>我叫嘉伟</h3>
                         <input value={this.state.bgColor} onChange={this.changeColor}/>
                     </div>);
              }
            }
             let app = document.getElementById("app");
            ReactDOM.render(<HelloWorld
            name="fat" color="yellow"/>,app);

React

标签:obj   struct   license   world   length   hang   target   OLE   turn   

原文地址:https://www.cnblogs.com/10manongit/p/12219101.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!