溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

react hooks?useWhyDidYouUpdate源碼分析

發(fā)布時(shí)間:2023-04-27 14:41:42 來(lái)源:億速云 閱讀:150 作者:iii 欄目:開發(fā)技術(shù)

這篇“react hooks useWhyDidYouUpdate源碼分析”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“react hooks useWhyDidYouUpdate源碼分析”文章吧。

hooks源碼來(lái)了

type IProps = Record<string, unknown>;
/**
 * 什么導(dǎo)致了頁(yè)面render自定義hooks
 *
 * @param componentName  觀測(cè)組件的名稱
 * @param props         需要觀測(cè)的數(shù)據(jù)(當(dāng)前組件 state 或者傳入的 props 等可能導(dǎo)致 rerender 的數(shù)據(jù))
 */
const useWhyDidYouUpdate = (componentName: any, props: any) => {
  // 創(chuàng)建一個(gè)ref對(duì)象
  let oldPropsRef = useRef<IProps>({});
  useEffect(() => {
    if (oldPropsRef.current) {
      // 遍歷新舊props的所有key
      let keys = Object.keys({ ...oldPropsRef.current, ...props });
      // 改變信息對(duì)象
      let changeMessageObj: IProps = {};
      keys.forEach((key) => {
        // 對(duì)比新舊props是否改變,改變及記錄到changeMessageObj
        if (!Object.is(oldPropsRef.current[key], props[key])) {
          changeMessageObj[key] = {
            from: oldPropsRef?.current[key],
            to: props[key],
          };
        }
      });
      // 是否存在改變信息,存在及打印
      if (Object.keys(changeMessageObj).length) {
        console.log(componentName, changeMessageObj);
      }
      // 更新ref
      oldPropsRef.current = props;
    }
  });
};

demo完整源碼

import React, { useState, useRef, useEffect } from 'react';
import { Button, Statistic } from 'antd';
type IProps = Record<string, unknown>;
/**
 * 什么導(dǎo)致了頁(yè)面render自定義hooks
 *
 * @param componentName  觀測(cè)組件的名稱
 * @param props         需要觀測(cè)的數(shù)據(jù)(當(dāng)前組件 state 或者傳入的 props 等可能導(dǎo)致 rerender 的數(shù)據(jù))
 */
const useWhyDidYouUpdate = (componentName: any, props: any) => {
  // 創(chuàng)建一個(gè)ref對(duì)象
  let oldPropsRef = useRef<IProps>({});
  useEffect(() => {
    if (oldPropsRef.current) {
      // 遍歷新舊props的所有key
      let keys = Object.keys({ ...oldPropsRef.current, ...props });
      // 改變信息對(duì)象
      let changeMessageObj: IProps = {};
      keys.forEach((key) => {
        // 對(duì)比新舊props是否改變,改變及記錄到changeMessageObj
        if (!Object.is(oldPropsRef.current[key], props[key])) {
          changeMessageObj[key] = {
            from: oldPropsRef?.current[key],
            to: props[key],
          };
        }
      });
      // 是否存在改變信息,存在及打印
      if (Object.keys(changeMessageObj).length) {
        console.log(componentName, changeMessageObj);
      }
      // 更新ref
      oldPropsRef.current = props;
    }
  });
};
// 演示demo
const Demo: React.FC<{ count: number }> = (props) => {
  useWhyDidYouUpdate('useWhyDidYouUpdateComponent', { ...props });
  return (
    <>
      <Statistic title="number:" value={props.count} />
    </>
  );
};
export default () => {
  const [count, setCount] = useState(0);
  return (
    <div>
      <Demo count={count} />
      <div>
        <Button
          type="primary"
          onClick={() => setCount((prevCount) => prevCount - 1)}
        >
          count -
        </Button>
        <Button
          type="primary"
          onClick={() => setCount((prevCount) => prevCount + 1)}
          style={{ marginLeft: 8 }}
        >
          count +
        </Button>
      </div>
    </div>
  );
};

以上就是關(guān)于“react hooks useWhyDidYouUpdate源碼分析”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI