广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >react学习每天一个hooks useWhyDidYouUpdate
  • 454
分享到

react学习每天一个hooks useWhyDidYouUpdate

react hooks useWhyDidYouUpdatereact hooks 2023-05-18 05:05:11 454人浏览 八月长安
摘要

目录先讲点废话来看看效果hooks源码来了demo完整源码先讲点废话 这个hooks我记忆很深,因为当时有一次面试的时候,叫我手写实现这个自定义hooks,哈哈,所以这个系列的第一个

先讲点废话

这个hooks我记忆很深,因为当时有一次面试的时候,叫我手写实现这个自定义hooks,哈哈,所以这个系列的第一个hooks 就准备是它了。

来看看效果

当我们的组件变得复杂的时候,你是不是想知道到底什么,导致了组件的渲染,或者值的变化是什么,这个hooks 就能解决你的问题。

hooks源码来了

type IProps = Record<string, unknown>;

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>;

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的详细内容,更多关于react hooks useWhyDidYouUpdate的资料请关注编程网其它相关文章!

--结束END--

本文标题: react学习每天一个hooks useWhyDidYouUpdate

本文链接: https://www.lsjlt.com/news/211683.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作