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