React優(yōu)化子組件render怎么用-創(chuàng)新互聯(lián)

這篇文章主要為大家展示了“React優(yōu)化子組件render怎么用”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“React優(yōu)化子組件render怎么用”這篇文章吧。

創(chuàng)新互聯(lián)公司長期為數(shù)千家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為泗陽企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計、做網(wǎng)站,泗陽網(wǎng)站改版等技術(shù)服務(wù)。擁有10余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

在react中,父組件的重新render會引發(fā)子組件的重新render,但是一些情況下我們會覺得這樣做有些多余,比如:

  1. 父組件并未傳遞props給子組件

  2. 新傳遞的props渲染結(jié)果不變

class A extends React.Component {
  render() {
    console.log('render')
    return <div>這是A組件</div>
  }
}

class Main extends React.Component {
  render() {
    return (
      <div>
        // 點擊button會讓A不斷調(diào)用render
        <button onClick={() => this.setState({ a: 1 })}>Main</button>
        <A />
      </div>
    )
  }
}

為了解決這個問題,需要分為ES6類組件和函數(shù)式組件兩種:

類組件

使用shouldComponentUpdate來對props和state進行判斷以此決定是否進行render

class A extends React.Component {
  shouldComponentUpdate(nextProps, nextState) {
    //兩次props對比
    return nextProps.a === this.props.a ? false : true
  }
  render() {
    console.log('render')
    return <div>這是A組件</div>
  }
}

class Main extends React.Component {
  // ...
  render() {
    return (
      <div>
        <button onClick={() => this.setState({ a: 1 })}>Main</button>
        <A a={this.state.a} />
      </div>
    )
  }
}

通過返回false來跳過這次更新

使用React.PureComponent,它與React.Component區(qū)別在于它已經(jīng)內(nèi)置了shouldComponentUpdate來對props和state進行淺對比,并跳過更新

//PureComponent
class A extends React.PureComponent {
  render() {
    console.log('render')
    return <div>這是A組件</div>
  }
}

class Main extends React.Component {
  state = {
    a: 1
  }
  render() {
    return (
      <div>
        <button onClick={() => this.setState({ a: 1 })}>Main</button>
        <A a={this.state.a} />
      </div>
    )
  }
}

函數(shù)組件

使用高階組件React.memo來包裹函數(shù)式組件,它和類組件的PureComponent類似,也是對對props進行淺比較決定是否更新

const A = props => {
  console.log('render A')
  return <div>這是A組件</div>
}
// React.memo包裹A
const B = React.memo(A)

const Main = props => {
  const [a, setA] = useState(1)
  console.log('render Main')

  return (
    <div>
      // 通過setA(a + 1)讓父組件重新render
      <button onClick={() => setA(a + 1)}>Main</button>
      // 一直傳入相同的props不會讓子組件重新render
      <B a={1} />
    </div>
  )
}

它的第二個參數(shù)接受一個兩次props作為參數(shù)的函數(shù),返回true則禁止子組件更新

其他

上面提到的淺比較就是根據(jù)內(nèi)存地址判斷是否相同:

// extends React.Component
class A extends React.Component {
  render() {
    console.log('render A')
    console.log(this.props)
    return <div>這是組件A</div>
  }
}

class Main extends React.Component {
  test = [1, 2, 3]
  render() {
    console.log('render Main')
    return (
      <div>
        <button
          onClick={() => {
            // 父組件render
            this.setState({})
            this.test.push(4)
          }}
        >
          Main
        </button>
        <A test={this.test} />
      </div>
    )
  }
}

結(jié)果是:

使用React.component:

React優(yōu)化子組件render怎么用

使用React.PureComponent:

React優(yōu)化子組件render怎么用

使用React.component,點擊之后子組件重新render。改為React.PureComponent之后,點擊button子組件并不會render。也因此,PureComponent根據(jù)前后內(nèi)存地址判斷是否相等,所以向子組件傳遞函數(shù)作為props時,使用內(nèi)聯(lián)箭頭函數(shù)的形式將會導(dǎo)致子組件的重新render;所以可以用箭頭函數(shù)作為成員變量的形式再將函數(shù)引用作為props傳遞。

以上是“React優(yōu)化子組件render怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

文章題目:React優(yōu)化子組件render怎么用-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://bm7419.com/article46/cedgeg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作App開發(fā)、網(wǎng)頁設(shè)計公司虛擬主機、面包屑導(dǎo)航、動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設(shè)