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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM