Loading homepage
Implement an optimized version of the useCounter React hook. Keep its existing counter API and state behavior while preserving utility method identities and avoiding a new result object when the count has not changed.
Use the JavaScript or TypeScript variant. The supplied tests render real React components and verify both state updates and reference identity across ordinary rerenders.
Input: useCounter() Output: count is 0 on the first render.
Input: useCounter(5); increment(); increment() in one event Output: count becomes 7 after React processes the batched updates.
Input: setCount((previous) => previous * 2); reset() Output: reset restores the original mount value, even if the initialValue argument has since changed.
Input: A parent rerenders while count is unchanged Output: The result object and all four methods retain their references.
Input: increment() changes count Output: The result object is new, its count reflects the update, and all four methods retain their references.
A React hook with the same counter API as useCounter, plus stable methods and a memoized result object during ordinary mounted rerenders.