[React]Class based and Functional based transform




Class based

class MyButton extends React.Component{
  
  //init
  constructor(props) {
    super(props);
    this.state ={btnText:"click me"}
    this._clk = this.clk.bind(this)
  }

  clk(){
    this.setState({
  btnText:"click!"
   });
  }
  

  render(){
    return(
      <div>
        <button type="button" id="123" onClick={this._clk}>{this.state.btnText}</button>
      </div>
    )
  }
}


ReactDOM.render(
 <MyButton />,
    document.getElementById('root')
);

Functional based

var NewComponent = React.createClass({

  getInitialState: function(){
  return {
   textContent:"click me"
   };
 },

  _event:function(e){
   console.log("click");
   console.log(e.target.textContent);
   this.setState({
  textContent:"click!"
   });
  },

  render: function() {
    return (
      <div>
        <button type="button" id="123" onClick={this._event}>{this.state.textContent}</button>
      </div>
    );
  }
});


ReactDOM.render(
 React.createElement(NewComponent,{
  }),
    document.getElementById('root')
);

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料