Given this simple example:
import React, { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
const handleClick = () => {
setCount(count + 1);
};
console.log("Component rerendered");
return (
<div>
<h1>{count}</h1>
<button onClick={handleClick}>Increment</button>
</div>
);
}
export default Counter;`
I want this component to re-render when Count equals X number.
The problem at hand is a component that has hundreds of re-renders due to necessary state updates. I wish to have a better control of these re-renders due to performance issues.
2
Answers
One possible solution might be to track the count with a ref from useRef.
Try this: