@recharts/devtools 0.0.6 → 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.
@@ -25,7 +25,7 @@ const isValidInspectorKey = (key) => {
25
25
  return key != null && key in INSPECTORS;
26
26
  };
27
27
  function useSelectedInspector() {
28
- const [selectedInspectorId, setSelectedInspectorId] = useSessionStorageState('useChartWidth | useChartHeight');
28
+ const [selectedInspectorId, setSelectedInspectorId] = useSessionStorageState('selectedRechartsDevtoolsInspector', 'useChartWidth | useChartHeight');
29
29
  const selectedInspector = INSPECTORS[selectedInspectorId];
30
30
  if (!selectedInspector) {
31
31
  setSelectedInspectorId('useChartWidth | useChartHeight');
@@ -47,7 +47,7 @@ export const RechartsDevtools = () => {
47
47
  }
48
48
  }
49
49
  }, [portalId, setSelectedInspectorId]);
50
- const [isOverlayEnabled, setIsOverlayEnabled] = useSessionStorageState(false);
50
+ const [isOverlayEnabled, setIsOverlayEnabled] = useSessionStorageState('rechartsDevtoolsOverlayEnabled', false);
51
51
  if (!container || !selectedInspector) {
52
52
  return null;
53
53
  }
@@ -1,7 +1,6 @@
1
1
  /**
2
2
  * This hook works just the same as `useState`, but it also stores the state in the session storage.
3
- * It uses `useId` from React to generate a unique key for the state.
4
3
  *
5
4
  * In browsers without session storage support, it will fallback to `useState`.
6
5
  */
7
- export declare const useSessionStorageState: <T>(initialState: T) => readonly [any, import("react").Dispatch<any>];
6
+ export declare const useSessionStorageState: <T>(id: string, initialState: T) => readonly [any, import("react").Dispatch<any>];
@@ -1,12 +1,10 @@
1
- import { useId, useState, useEffect } from "react";
1
+ import { useState, useEffect } from 'react';
2
2
  /**
3
3
  * This hook works just the same as `useState`, but it also stores the state in the session storage.
4
- * It uses `useId` from React to generate a unique key for the state.
5
4
  *
6
5
  * In browsers without session storage support, it will fallback to `useState`.
7
6
  */
8
- export const useSessionStorageState = (initialState) => {
9
- const id = useId();
7
+ export const useSessionStorageState = (id, initialState) => {
10
8
  const [state, setState] = useState(() => {
11
9
  try {
12
10
  const storedState = sessionStorage.getItem(id);
@@ -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';
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "access": "public"
5
5
  },
6
6
  "type": "module",
7
- "version": "0.0.6",
7
+ "version": "0.0.8",
8
8
  "description": "Devtools and helper functions for Recharts",
9
9
  "main": "dist/index.js",
10
10
  "types": "dist/index.d.ts",