@recharts/devtools 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This hook is used to log component re-renders in development mode.
|
|
3
|
+
* It is useful for debugging performance issues and understanding why a component is re-rendering.
|
|
4
|
+
* It should only be used in development mode and not in production.
|
|
5
|
+
* @param componentName The name of the component to log
|
|
6
|
+
* @param props The props of the component
|
|
7
|
+
* @param hooks Optional hooks or other properties to observe for changes
|
|
8
|
+
* @returns void
|
|
9
|
+
*/
|
|
10
|
+
export declare function useWhyDidYouRender<T extends Record<string, any>>(componentName: string, props: T, hooks?: Record<string, any>): void;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* This hook is used to log component re-renders in development mode.
|
|
4
|
+
* It is useful for debugging performance issues and understanding why a component is re-rendering.
|
|
5
|
+
* It should only be used in development mode and not in production.
|
|
6
|
+
* @param componentName The name of the component to log
|
|
7
|
+
* @param props The props of the component
|
|
8
|
+
* @param hooks Optional hooks or other properties to observe for changes
|
|
9
|
+
* @returns void
|
|
10
|
+
*/
|
|
11
|
+
export function useWhyDidYouRender(componentName, props, hooks) {
|
|
12
|
+
const previousProps = useRef();
|
|
13
|
+
const previousHooks = useRef();
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
const allProps = { ...props, ...hooks };
|
|
16
|
+
if (previousProps.current) {
|
|
17
|
+
const previousAllProps = { ...previousProps.current, ...previousHooks.current };
|
|
18
|
+
const allKeys = Object.keys({ ...allProps, ...previousAllProps });
|
|
19
|
+
const changesObj = {};
|
|
20
|
+
allKeys.forEach((key) => {
|
|
21
|
+
if (previousAllProps[key] !== allProps[key]) {
|
|
22
|
+
changesObj[key] = {
|
|
23
|
+
from: previousAllProps[key],
|
|
24
|
+
to: allProps[key],
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
const changedKeys = Object.keys(changesObj);
|
|
29
|
+
if (changedKeys.length) {
|
|
30
|
+
const prev = Object.fromEntries(changedKeys.map((key) => [key, previousAllProps[key]]));
|
|
31
|
+
const curr = Object.fromEntries(changedKeys.map((key) => [key, allProps[key]]));
|
|
32
|
+
console.log('[why-did-you-render?]', componentName, Object.keys(changesObj).join(', '), {
|
|
33
|
+
prev,
|
|
34
|
+
curr,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
console.log('[why-did-you-render?]', componentName, 'initial render', allProps);
|
|
40
|
+
}
|
|
41
|
+
previousProps.current = props;
|
|
42
|
+
previousHooks.current = hooks;
|
|
43
|
+
});
|
|
44
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -4,3 +4,4 @@ export { generateMockMarketData } from './generateMockMarketData.js';
|
|
|
4
4
|
export { RechartsDevtools } from './components/RechartsDevtools.js';
|
|
5
5
|
export { RechartsDevtoolsPortal, RechartsDevtoolsContext, } from './context/RechartsDevtoolsContext.js';
|
|
6
6
|
export { RECHARTS_DEVTOOLS_PORTAL_ID } from './constants.js';
|
|
7
|
+
export { useWhyDidYouRender } from './hooks/useWhyDidYouRender.js';
|
package/dist/index.js
CHANGED
|
@@ -4,3 +4,4 @@ export { generateMockMarketData } from './generateMockMarketData.js';
|
|
|
4
4
|
export { RechartsDevtools } from './components/RechartsDevtools.js';
|
|
5
5
|
export { RechartsDevtoolsPortal, RechartsDevtoolsContext, } from './context/RechartsDevtoolsContext.js';
|
|
6
6
|
export { RECHARTS_DEVTOOLS_PORTAL_ID } from './constants.js';
|
|
7
|
+
export { useWhyDidYouRender } from './hooks/useWhyDidYouRender.js';
|