react學(xué)習(xí)每天一個hooks?useWhyDidYouUpdate
先講點廢話
這個hooks我記憶很深,因為當(dāng)時有一次面試的時候,叫我手寫實現(xiàn)這個自定義hooks,哈哈,所以這個系列的第一個hooks 就準(zhǔn)備是它了。
來看看效果
當(dāng)我們的組件變得復(fù)雜的時候,你是不是想知道到底什么,導(dǎo)致了組件的渲染,或者值的變化是什么,這個hooks 就能解決你的問題。
hooks源碼來了
type IProps = Record<string, unknown>; /** * 什么導(dǎo)致了頁面render自定義hooks * * @param componentName 觀測組件的名稱 * @param props 需要觀測的數(shù)據(jù)(當(dāng)前組件 state 或者傳入的 props 等可能導(dǎo)致 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>; /** * 什么導(dǎo)致了頁面render自定義hooks * * @param componentName 觀測組件的名稱 * @param props 需要觀測的數(shù)據(jù)(當(dāng)前組件 state 或者傳入的 props 等可能導(dǎo)致 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學(xué)習(xí)每天一個hooks useWhyDidYouUpdate的詳細(xì)內(nèi)容,更多關(guān)于react hooks useWhyDidYouUpdate的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
react native 原生模塊橋接的簡單說明小結(jié)
這篇文章主要介紹了react native 原生模塊橋接的簡單說明小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02react實現(xiàn)一個優(yōu)雅的圖片占位模塊組件詳解
這篇文章主要給大家介紹了關(guān)于react如何實現(xiàn)一個還算優(yōu)雅的占位模塊圖片組件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10React styled-components設(shè)置組件屬性的方法
這篇文章主要介紹了styled-components設(shè)置組件屬性的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08詳解在React-Native中持久化redux數(shù)據(jù)
這篇文章主要介紹了在React-Native中持久化redux數(shù)據(jù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05