react學習每天一個hooks?useWhyDidYouUpdate
先講點廢話
這個hooks我記憶很深,因為當時有一次面試的時候,叫我手寫實現(xiàn)這個自定義hooks,哈哈,所以這個系列的第一個hooks 就準備是它了。
來看看效果
當我們的組件變得復雜的時候,你是不是想知道到底什么,導致了組件的渲染,或者值的變化是什么,這個hooks 就能解決你的問題。

hooks源碼來了
type IProps = Record<string, unknown>;
/**
* 什么導致了頁面render自定義hooks
*
* @param componentName 觀測組件的名稱
* @param props 需要觀測的數(shù)據(jù)(當前組件 state 或者傳入的 props 等可能導致 rerender 的數(shù)據(jù))
*/
const useWhyDidYouUpdate = (componentName: any, props: any) => {
// 創(chuàng)建一個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 需要觀測的數(shù)據(jù)(當前組件 state 或者傳入的 props 等可能導致 rerender 的數(shù)據(jù))
*/
const useWhyDidYouUpdate = (componentName: any, props: any) => {
// 創(chuàng)建一個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的詳細內(nèi)容,更多關(guān)于react hooks useWhyDidYouUpdate的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
react-native中ListView組件點擊跳轉(zhuǎn)的方法示例
ListView作為React Native的核心組件,用于高效地顯示一個可以垂直滾動的變化的數(shù)據(jù)列表。下面這篇文章主要給大家介紹了關(guān)于react-native中ListView組件點擊跳轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09
React關(guān)于antd table中select的設(shè)值更新問題
這篇文章主要介紹了React關(guān)于antd table中select的設(shè)值更新問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
一文詳解ReactNative狀態(tài)管理redux-toolkit使用
這篇文章主要為大家介紹了ReactNative狀態(tài)管理redux-toolkit使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
React Hook 父子組件相互調(diào)用函數(shù)方式
這篇文章主要介紹了React Hook 父子組件相互調(diào)用函數(shù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09

