react之shouldComponentUpdate简单定制数据更新

import React from 'react'

class Demo extends React.Component{
  constructor(props){
    super(props)
    this.state={count:1}
    this.handleClick=this.handleClick.bind(this)
  }
  handleClick(){
    this.setState({count:this.state.count+1})
  }
  shouldComponentUpdate(){
    if(this.state.count%5==0){

      return true
    }
    return false
  }
  render(){
    return (
      <div>
        {this.state.count}<br />
        <button onClick={this.handleClick}>点击</button>
      </div>

    )
  }
}
export default Demo

只有当count等于5时,视图上才会更新数据

posted @ 2018-09-22 14:09  raindi  阅读(2060)  评论(0编辑  收藏  举报