A hook that prints the number of renders for a given component, along with a timestamp of the most recent render and the time elapsed since the last render.
- Easily display information on components render
import { Typography } from 'antd';
import useInterval from 'beautiful-react-hooks/useInterval';
import useRenderInfo from 'beautiful-react-hooks/useRenderInfo';
const RenderInfo = () => {
const [seconds, setSeconds] = React.useState(0);
// repeat the function each 1000ms
useInterval(() => {
setSeconds(1 + seconds);
}, 1000);
useRenderInfo('Module');
return (
<DisplayDemo title="useRenderInfo">
<Typography.Paragraph>Check the console!</Typography.Paragraph>
</DisplayDemo>
);
};
<RenderInfo />
import { Typography } from 'antd';
import useInterval from 'beautiful-react-hooks/useInterval';
import useRenderInfo from 'beautiful-react-hooks/useRenderInfo';
const RenderInfo = () => {
const [seconds, setSeconds] = React.useState(0);
const info = useRenderInfo();
// repeat the function each 1000ms
useInterval(() => {
setSeconds(1 + seconds);
}, 1000);
return (
<DisplayDemo title="useRenderInfo">
<Typography.Paragraph>{info.sinceLast} seconds passed from the last render!</Typography.Paragraph>
</DisplayDemo>
);
};
<RenderInfo />
- When debugging a component
- In production build, you don't want useless logs in console :)
export interface RenderInfo {
readonly module: string;
renders: number;
timestamp: null | number;
sinceLast: null | number | '[now]';
}
/**
* useRenderInfo
* @param moduleName
* @param log
* @returns {{renders: number, module: *, timestamp: null}}
*/
declare const useRenderInfo: (moduleName?: string, log?: boolean) => RenderInfo;
export default useRenderInfo;