hooks: useRenderCount
Debug better with this utility hookDebugging is essential to software development, but it can often be frustrating and time-consuming. Fortunately, there are tools and techniques that can make the process easier and more efficient.
Table of Contents
useRenderCount
Using this simple utility hook, you can quickly identify which components are rendering too frequently, and optimize your code to improve performance.
import { useEffect, useRef } from "react";
export default function useRenderCount() { const countRef = useRef < number > 1; useEffect(() => countRef.current++); return countRef.current;}Example of usage
import useRenderCount from "./useRenderCount"
export default function ExampleComponent{ const [show, setShow] = useState(false);
const renderCount = useRenderCount();
return ( <> <div>{renderCount}</div> <button onClick={() => setShow(!show)}>Toggle</button> </> )}Related snippets
- Intl API: Number Formatting September 10, 2022 Format numbers like 5.2K or 1.7M using the JavaScript Internationalization API
- Package Alias: Multiple versions of npm package August 17, 2022 Working with custom alias
- HTTP2: Server Push (Node.js) November 1, 2017 Faster app loading with resource pushing