温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

react hooks useWhyDidYouUpdate源码分析

发布时间:2023-04-27 14:41:42 来源:亿速云 阅读:103 作者:iii 栏目:开发技术

这篇“react hooks useWhyDidYouUpdate源码分析”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“react hooks useWhyDidYouUpdate源码分析”文章吧。

hooks源码来了

type IProps = Record<string, unknown>;
/**
 * 什么导致了页面render自定义hooks
 *
 * @param componentName  观测组件的名称
 * @param props         需要观测的数据(当前组件 state 或者传入的 props 等可能导致 rerender 的数据)
 */
const useWhyDidYouUpdate = (componentName: any, props: any) => {
  // 创建一个ref对象
  let oldPropsRef = useRef<IProps>({});
  useEffect(() => {
    if (oldPropsRef.current) {
      // 遍历新旧props的所有key
      let keys = Object.keys({ ...oldPropsRef.current, ...props });
      // 改变信息对象
      let changeMessageObj: IProps = {};
      keys.forEach((key) => {
        // 对比新旧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>;
/**
 * 什么导致了页面render自定义hooks
 *
 * @param componentName  观测组件的名称
 * @param props         需要观测的数据(当前组件 state 或者传入的 props 等可能导致 rerender 的数据)
 */
const useWhyDidYouUpdate = (componentName: any, props: any) => {
  // 创建一个ref对象
  let oldPropsRef = useRef<IProps>({});
  useEffect(() => {
    if (oldPropsRef.current) {
      // 遍历新旧props的所有key
      let keys = Object.keys({ ...oldPropsRef.current, ...props });
      // 改变信息对象
      let changeMessageObj: IProps = {};
      keys.forEach((key) => {
        // 对比新旧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>
  );
};

以上就是关于“react hooks useWhyDidYouUpdate源码分析”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI