react學(xué)習(xí)每天一個(gè)hooks?useWhyDidYouUpdate
先講點(diǎn)廢話(huà)
這個(gè)hooks我記憶很深,因?yàn)楫?dāng)時(shí)有一次面試的時(shí)候,叫我手寫(xiě)實(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è)組件的名稱(chēng)
* @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è)組件的名稱(chēng)
* @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 native 原生模塊橋接的簡(jiǎn)單說(shuō)明小結(jié)
這篇文章主要介紹了react native 原生模塊橋接的簡(jiǎn)單說(shuō)明小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
react實(shí)現(xiàn)一個(gè)優(yōu)雅的圖片占位模塊組件詳解
這篇文章主要給大家介紹了關(guān)于react如何實(shí)現(xiàn)一個(gè)還算優(yōu)雅的占位模塊圖片組件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
React性能優(yōu)化的實(shí)現(xiàn)方法詳解
react憑借virtual DOM和diff算法擁有高效的性能,除此之外也有很多其他的方法和技巧可以進(jìn)一步提升react性能,在本文中我將列舉出可有效提升react性能的幾種方法,幫助我們改進(jìn)react代碼,提升性能2023-01-01
React styled-components設(shè)置組件屬性的方法
這篇文章主要介紹了styled-components設(shè)置組件屬性的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
詳解在React-Native中持久化redux數(shù)據(jù)
這篇文章主要介紹了在React-Native中持久化redux數(shù)據(jù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05
詳解在React項(xiàng)目中如何集成和使用web worker
在復(fù)雜的React應(yīng)用中,某些計(jì)算密集型或耗時(shí)操作可能會(huì)阻塞主線程,導(dǎo)致用戶(hù)界面出現(xiàn)卡頓或響應(yīng)慢的現(xiàn)象,為了優(yōu)化用戶(hù)體驗(yàn),可以采用Web Worker來(lái)在后臺(tái)線程中執(zhí)行這些操作,本文將詳細(xì)介紹在React項(xiàng)目中如何集成和使用Web Worker來(lái)改善應(yīng)用性能,需要的朋友可以參考下2023-12-12

