加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

React

发布时间:2020-12-15 05:10:46 所属栏目:百科 来源:网络整理
导读: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.
<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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读