When you're chasing a performance bug, the first question is usually "how often is this component actually re-rendering?" useRenderCount answers it: a tiny debugging hook that returns how many times the component has rendered so far — 1 on mount, 2 after the next render, and so on. Drop it in, log the number, and watch whether a memo or a bad dependency is causing render storms.
Implement useRenderCount(). It takes no arguments and returns the running render count including the current render. Critically, counting must not itself cause a re-render — so no state.
function useRenderCount() {
// returns the number of times this component has rendered (>= 1)
}
function Row({ item }) {
const renders = useRenderCount();
console.log(`Row ${item.id} rendered ${renders} times`);
}
// render 1 -> 1, render 2 -> 2, render 3 -> 3
const renders = useRenderCount(); // 1 on mount, grows by 1 each render
useState to hold the count would schedule another render and inflate the number. A ref mutates silently.