react學(xué)習(xí)每天一個(gè)hooks?useWhyDidYouUpdate
先講點(diǎn)廢話
這個(gè)hooks我記憶很深,因?yàn)楫?dāng)時(shí)有一次面試的時(shí)候,叫我手寫實(shí)現(xiàn)這個(gè)自定義hooks,哈哈,所以這個(gè)系列的第一個(gè)hooks 就準(zhǔn)備是它了。
來(lái)看看效果
當(dāng)我們的組件變得復(fù)雜的時(shí)候,你是不是想知道到底什么,導(dǎo)致了組件的渲染,或者值的變化是什么,這個(gè)hooks 就能解決你的問(wèn)題。

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>
);
};以上就是react學(xué)習(xí)每天一個(gè)hooks useWhyDidYouUpdate的詳細(xì)內(nèi)容,更多關(guān)于react hooks useWhyDidYouUpdate的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
為react組件庫(kù)添加typescript類型提示的方法
這篇文章主要介紹了為react組件庫(kù)添加typescript類型提示,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
React實(shí)現(xiàn)全局組件的Toast輕提示效果
這篇文章主要介紹了React實(shí)現(xiàn)全局組件的Toast輕提示效果,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
React?Native?Modal?的封裝與使用實(shí)例詳解
這篇文章主要介紹了React?Native?Modal?的封裝與使用,本文通過(guò)實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
React中項(xiàng)目路由配置與跳轉(zhuǎn)方法詳解
這篇文章主要為大家詳細(xì)介紹了React中項(xiàng)目路由配置與跳轉(zhuǎn)方法的相關(guān)資料,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-08-08
react-native中ListView組件點(diǎn)擊跳轉(zhuǎn)的方法示例
ListView作為React Native的核心組件,用于高效地顯示一個(gè)可以垂直滾動(dòng)的變化的數(shù)據(jù)列表。下面這篇文章主要給大家介紹了關(guān)于react-native中ListView組件點(diǎn)擊跳轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-09-09
React關(guān)于antd table中select的設(shè)值更新問(wèn)題
這篇文章主要介紹了React關(guān)于antd table中select的設(shè)值更新問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
React實(shí)現(xiàn)反向代理和修改打包后的目錄
這篇文章主要介紹了React實(shí)現(xiàn)反向代理和修改打包后的目錄方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
一文詳解ReactNative狀態(tài)管理redux-toolkit使用
這篇文章主要為大家介紹了ReactNative狀態(tài)管理redux-toolkit使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
React Hook 父子組件相互調(diào)用函數(shù)方式
這篇文章主要介紹了React Hook 父子組件相互調(diào)用函數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09

