国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

React.memo、shouldComponentUpdate、PureComponent的基本使用

這篇具有很好參考價(jià)值的文章主要介紹了React.memo、shouldComponentUpdate、PureComponent的基本使用。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

React.memo

當(dāng)我們組件內(nèi)部有大量的計(jì)算是,避免組件內(nèi)部進(jìn)行不必要的重新渲染,使用React.memo進(jìn)行緩存組件,避免不必要的重新渲染

React.memo是用來判斷是否需要重新渲染組件,和shouldComponentUpdate的區(qū)別是
shouldComponentUpdate 用于class組件方式,而React.memo 用于hooks方式

語法

React.memo(Component, propsAreEqual)

  • Component 需要緩存的組件
  • propsAreEqual兩個(gè)props是否相等、當(dāng)返回true時(shí),那么組件就會(huì)復(fù)用,不重新渲染組件
 function memo<P extends object>(
        Component: FunctionComponent<P>,
        propsAreEqual?: (prevProps: Readonly<PropsWithChildren<P>>, nextProps: Readonly<PropsWithChildren<P>>) => boolean
    ): NamedExoticComponent<P>;
    function memo<T extends ComponentType<any>>(
        Component: T,
        propsAreEqual?: (prevProps: Readonly<ComponentProps<T>>, nextProps: Readonly<ComponentProps<T>>) => boolean
    ): MemoExoticComponent<T>;

使用

默認(rèn)根據(jù)props的淺比較進(jìn)行來判斷子組件是否更新

import React, { useState, useRef, useEffect, useMemo } from 'react';
const  LiveInfo = (props) => {
  return <div>
    LiveInfo
  </div>
}
export default React.memo(LiveInfo);

傳遞第二個(gè)參數(shù)是就根據(jù)第一個(gè)參數(shù)返回值判斷

import React, { useState, useRef, useEffect, useMemo } from 'react';
const  LiveInfo = (props) => {
  return <div>
    LiveInfo
  </div>
}
export default React.memo(LiveInfo, function propsAreEqual(prevProps, nextProps) {
	if (prevProps.visbile !== nextProps.visible) {
		return false // 更新
	}
	return true // 復(fù)用,不重新渲染
});

shouldComponentUpdate

是否應(yīng)該更新組件,true更新、false不更新

shouldComponentUpdate(nextProps, nextState)

使用

export default class PromiseRender extends React.Component {
  state = {
    component: null,
  };

  shouldComponentUpdate = (nextProps, nextState) => {
    const { component } = this.state;
    if (nextState.component !== component) return true; // 更新
    return false; // 不更新
  };

  render() {
    const { component: Component } = this.state;
    const {  ...rest } = this.props;
    return Component ? (
      <Component {...rest} />
    ) : (
      <div
        style={{
          width: '100%',
          height: '100%',
          margin: 'auto',
          paddingTop: 50,
          textAlign: 'center',
        }}
      >
        
      </div>
    );
  }
}

React.PureComponent

改寫了 shouldComponentUpdate,添加上了淺比較

PureComponent 也就是改寫了 shouldComponentUpdate ,加上了淺比較,React源碼:

if (type.prototype && type.prototype.isPureReactComponent) {
 return (
   !shallowEqual(oldProps, newProps) || !shallowEqual(oldState, newState)
 );
}

shallowEqual 源碼:文章來源地址http://www.zghlxwxcb.cn/news/detail-456673.html

const hasOwn = Object.prototype.hasOwnProperty

function is(x, y) {
  if (x === y) {
    return x !== 0 || y !== 0 || 1 / x === 1 / y
  } else {
    return x !== x && y !== y
  }
}

export default function shallowEqual(objA, objB) {
  if (is(objA, objB)) return true

  if (typeof objA !== 'object' || objA === null ||
      typeof objB !== 'object' || objB === null) {
    return false
  }

  const keysA = Object.keys(objA)
  const keysB = Object.keys(objB)

  if (keysA.length !== keysB.length) return false

  for (let i = 0; i < keysA.length; i++) {
    if (!hasOwn.call(objB, keysA[i]) ||
        !is(objA[keysA[i]], objB[keysA[i]])) {
      return false
    }
  }

  return true
}

到了這里,關(guān)于React.memo、shouldComponentUpdate、PureComponent的基本使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • React16源碼: Component與PureComponent源碼實(shí)現(xiàn)

    概述 Component 就是組件, 這個(gè)概念依托于最直觀的在react上的一個(gè)表現(xiàn),那就是 React.Component 我們寫的組件大都是繼承于 React.Component 這個(gè)baseClass 而寫的類 這個(gè)類代表著我們使用 react 去實(shí)現(xiàn)的一個(gè)組件 那么在react當(dāng)中不僅僅只有 Component 這一個(gè)baseClass,還有另外一個(gè)叫 PureComp

    2024年02月03日
    瀏覽(15)
  • 手寫對象淺比較(React中pureComponent和Component區(qū)別)

    手寫對象淺比較(React中pureComponent和Component區(qū)別)

    ? ? ? ? PureComponent會(huì)給類組件默認(rèn)加一個(gè)shouldComponentUpdate這樣的周期函數(shù) ? 在此周期函數(shù)中,它對新老的屬性/狀態(tài) 會(huì)做一個(gè)淺比較 ? 如果經(jīng)過淺比較,發(fā)現(xiàn)屬性和狀態(tài)并沒有改變,則返回false(也就是不繼續(xù)更新組件),有變化才會(huì)去更新?。?當(dāng)使用component時(shí),父組件的

    2024年02月16日
    瀏覽(25)
  • 5.React.memo 性能優(yōu)化

    性能優(yōu)化, React.memo 2. React.memo類似純組件,可提高組件性能表現(xiàn)(類組件PureComponent)

    2024年02月11日
    瀏覽(24)
  • React性能優(yōu)化之Memo、useMemo

    React 的渲染機(jī)制,組件內(nèi)部的 state 或者 props 一旦發(fā)生修改,整個(gè)組件樹都會(huì)被重新渲染一次,即時(shí)子組件的參數(shù)沒有被修改,甚至無狀態(tài)組件 會(huì)造成性能浪費(fèi) React.memo 是 React 官方提供的一個(gè)高階組件,用于緩存我們的需要優(yōu)化的組件 React 中的組件被設(shè)計(jì)為在狀態(tài)或 props 值

    2024年02月14日
    瀏覽(20)
  • React性能優(yōu)化之memo緩存函數(shù)

    React是一個(gè)非常流行的前端框架,但是在處理大型應(yīng)用程序時(shí),性能可能會(huì)成為一個(gè)問題。為了解決這個(gè)問題,React提供了一個(gè)稱為memo的功能,它可以緩存函數(shù)并避免不必要的重新渲染。 memo是React中的一個(gè)高階組件(HOC),它接收一個(gè)組件并返回一個(gè)新的組件。這個(gè)新組件具

    2024年02月11日
    瀏覽(46)
  • React.Memo和useMemo的區(qū)別?

    hello world歡迎來到前端的新世界 ??當(dāng)前文章系列專欄:react.js ?????博主在前端領(lǐng)域還有很多知識和技術(shù)需要掌握,正在不斷努力填補(bǔ)技術(shù)短板。(如果出現(xiàn)錯(cuò)誤,感謝大家指出)?? ??感謝大家支持!您的觀看就是作者創(chuàng)作的動(dòng)力 React.memo和useMemo是React中兩個(gè)不同的特性,用

    2024年02月06日
    瀏覽(21)
  • React.memo每天一個(gè)小知識,有例子

    react的子組件props接收父組件的屬性 運(yùn)用場景:子組件如果接收多個(gè)參數(shù),父組件傳的值有變化,就更新(多次請求接口,再有圖片請求的時(shí)候,費(fèi)勁),比如打開彈出框,open就是true/false, 但是你只想在單據(jù)不一樣的時(shí)候更新,就需要自定義邏輯, 用了React.memo回調(diào)函數(shù)判斷。

    2024年01月19日
    瀏覽(37)
  • React【性能優(yōu)化_shouldComponentUpdate、性能優(yōu)化_時(shí)間分片、性能優(yōu)化_虛擬列表、PropTypes 進(jìn)行類型檢查、默認(rèn) Prop 值、 TypeScript】(六)

    React【性能優(yōu)化_shouldComponentUpdate、性能優(yōu)化_時(shí)間分片、性能優(yōu)化_虛擬列表、PropTypes 進(jìn)行類型檢查、默認(rèn) Prop 值、 TypeScript】(六)

    目錄 性能優(yōu)化_shouldComponentUpdate 性能優(yōu)化_時(shí)間分片 性能優(yōu)化_虛擬列表

    2024年02月08日
    瀏覽(28)
  • React Native Ref轉(zhuǎn)發(fā)/Memo緩存/HOC高階組件/Context上下文

    React Native Ref轉(zhuǎn)發(fā)/Memo緩存/HOC高階組件/Context上下文

    1、使用自定義組件時(shí),實(shí)現(xiàn)外層組件對原始組件(TextInput)的操作 外層組件使用 ref 屬性 子組件使用 forwardRef 包裹 2、函數(shù)式組件對外暴露實(shí)例方法(cusomFocus) 子組件 父組件如圖一所示 1 、 避免多余渲染 問題:每次點(diǎn)擊按鈕都會(huì)導(dǎo)致 InfoView 組件發(fā)生重繪,即使每次 setI

    2024年01月21日
    瀏覽(25)
  • react---react router 5 基本使用

    react---react router 5 基本使用

    目錄 1.路由介紹 2.路由使用 3.路由組件和一般組件 4.Switch 單一匹配 5.解決二級路由樣式丟失的問題 6.路由精準(zhǔn)匹配和模糊匹配 7.Redirect路由重定向 1.路由介紹 路由是根據(jù)不同的 URL 地址展示不同的內(nèi)容或頁面,在 SPA 應(yīng)用中,大部分頁面結(jié)果不改變,只改變部分內(nèi)容的使用。

    2024年02月09日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包