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

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

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

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

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

  2. 新傳遞的props渲染結果不變

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

class Main extends React.Component {
  render() {
    return (
      <div>
        // 點擊button會讓A不斷調用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ū)別在于它已經內置了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則禁止子組件更新

其他

上面提到的淺比較就是根據內存地址判斷是否相同:

// 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>
    )
  }
}

結果是:

使用React.component:

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

使用React.PureComponent:

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

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

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

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

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

廣告

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

手機網站建設