最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

React優(yōu)化子組件render的使用

 更新時(shí)間:2019年05月12日 09:24:37   作者:RThong  
這篇文章主要介紹了React優(yōu)化子組件render的使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

在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>
        // 點(diǎn)擊button會讓A不斷調(diào)用render
        <button onClick={() => this.setState({ a: 1 })}>Main</button>
        <A />
      </div>
    )
  }
}

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

類組件

使用shouldComponentUpdate來對props和state進(jìn)行判斷以此決定是否進(jìn)行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進(jìn)行淺對比,并跳過更新

//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進(jìn)行淺比較決定是否更新

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

它的第二個(gè)參數(shù)接受一個(gè)兩次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.PureComponent:

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 在 React 項(xiàng)目中使用 Auth0 并集成到后端服務(wù)的配置步驟詳解

    在 React 項(xiàng)目中使用 Auth0 并集成到后端服務(wù)的配置步驟詳解

    這篇文章主要介紹了在 React 項(xiàng)目中使用 Auth0 并集成到后端服務(wù)的配置步驟詳解,通過本文詳細(xì)步驟,您可以將 Auth0 集成到 React 項(xiàng)目并與后端服務(wù)交互,需要的朋友可以參考下
    2024-07-07
  • svelte5中使用react組件的方法

    svelte5中使用react組件的方法

    本文介紹了如何在Svelte 5項(xiàng)目中導(dǎo)入并使用React組件庫,并提供了一個(gè)示例項(xiàng)目地址,此外,還添加了一個(gè)React組件庫(如Ant Design)的示例,感興趣的朋友跟隨小編一起看看吧
    2025-01-01
  • React懶加載實(shí)現(xiàn)原理深入分析

    React懶加載實(shí)現(xiàn)原理深入分析

    懶加載意思是不會預(yù)加載,而是需要使用某段代碼,某個(gè)組件或者某張圖片時(shí),才加載他們(延遲加載),這篇文章主要介紹了React懶加載實(shí)現(xiàn)原理
    2022-11-11
  • react render props模式實(shí)現(xiàn)組件復(fù)用示例

    react render props模式實(shí)現(xiàn)組件復(fù)用示例

    本文主要介紹了react render props模式實(shí)現(xiàn)組件復(fù)用示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • react基于Ant Desgin Upload實(shí)現(xiàn)導(dǎo)入導(dǎo)出

    react基于Ant Desgin Upload實(shí)現(xiàn)導(dǎo)入導(dǎo)出

    本文主要介紹了react基于Ant Desgin Upload實(shí)現(xiàn)導(dǎo)入導(dǎo)出,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01
  • React使用TailwindCSS的實(shí)現(xiàn)示例

    React使用TailwindCSS的實(shí)現(xiàn)示例

    TailwindCSS是一個(gè)實(shí)用優(yōu)先的CSS框架,本文主要介紹了React使用TailwindCSS的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12
  • React常見跨窗口通信方式實(shí)例詳解

    React常見跨窗口通信方式實(shí)例詳解

    這篇文章主要為大家介紹了React常見跨窗口通信方式實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • 使用React Profiler進(jìn)行性能優(yōu)化方案詳解

    使用React Profiler進(jìn)行性能優(yōu)化方案詳解

    在現(xiàn)代前端開發(fā)中,性能優(yōu)化是一個(gè)不可忽視的重要環(huán)節(jié),在 React 生態(tài)系統(tǒng)中,React Profiler 是一個(gè)強(qiáng)大的工具,下面我們來看看如何使用它來提升我們的 React 應(yīng)用吧
    2025-03-03
  • React使用fullpage.js實(shí)現(xiàn)整屏翻頁功能

    React使用fullpage.js實(shí)現(xiàn)整屏翻頁功能

    最近筆者在完成一個(gè)移動(dòng)端小項(xiàng)目的過程中需要實(shí)現(xiàn)整屏翻頁的效果;調(diào)研完畢之后,最終決定使用pullPage.js實(shí)現(xiàn)此功能,fullPage.js使用起來比較方便,并且官網(wǎng)上也給了在react項(xiàng)目中使用的demo,本文記錄了這個(gè)第三方庫的使用過程,感興趣的朋友可以參考下
    2023-11-11
  • react中通過props實(shí)現(xiàn)父子組件間通信的使用示例

    react中通過props實(shí)現(xiàn)父子組件間通信的使用示例

    在React中,父組件可以通過props屬性向子組件傳遞數(shù)據(jù),子組件可以通過props屬性接收父組件傳遞過來的數(shù)據(jù),本文就來介紹一下如何實(shí)現(xiàn),感興趣的可以了解一下
    2023-10-10

最新評論

准格尔旗| 德惠市| 汽车| 津市市| 海安县| 宜黄县| 商洛市| 宣武区| 屯留县| 阿拉善盟| 同江市| 平阳县| 横峰县| 阳谷县| 大竹县| 英山县| 古蔺县| 辰溪县| 哈巴河县| 疏勒县| 富顺县| 婺源县| 平谷区| 万源市| 沾化县| 南宫市| 盐边县| 永安市| 乌兰县| 高要市| 宜城市| 大关县| 太和县| 盐池县| 兴化市| 马龙县| 双城市| 隆昌县| 西平县| 辉县市| 岚皋县|