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时,视图上才会更新数据

原文地址:https://www.cnblogs.com/raind/p/9690030.html