美文网首页
react 用es6写法 以及问题

react 用es6写法 以及问题

作者: 海藻web开发 | 来源:发表于2017-06-08 16:13 被阅读0次
      class Count extends React.Component{
          constructor(props){
             super(props)
             this.state={count:5}
          }
          handleClick(event) {
            this.setState({count: ++this.state.count});
          }
          render() {
            return (
              <p onClick={this.handleClick.bind(this)}>
                {this.state.count}点击我,递增
              </p>
            );
          }
        }
        ReactDOM.render(<Count />,document.getElementById('example'));
    

    注意:点击事件,要使用bind(this) ,改变this指向 或者使用箭头函数

    相关文章

      网友评论

          本文标题:react 用es6写法 以及问题

          本文链接:https://www.haomeiwen.com/subject/illtqxtx.html